Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Sound Problem in my Game  (Read 537 times)
0 Members and 1 Guest are viewing this topic.
Offline BowtieStranger

Junior Newbie





« Posted 2013-07-14 18:18:15 »

Hey Folks!
I created a little Java Game and now i want to include Sound.
I created a Sound Class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
package game;

import java.applet.Applet;
import java.applet.AudioClip;

public class Musik {
   
   public static final Musik musik1 = new Musik("/src/game/Sound/Test.wav");
   
   private AudioClip clip;

   public Musik(String filename){
      try{
         clip = Applet.newAudioClip(Sound.class.getResource(filename));
     
      }catch(Exception e){
         e.printStackTrace();
      }
   }
   
   public void play(){
      try{
         new Thread(){
            public void run(){
               clip.play();
            }
         
         }.start();  
         }catch(Exception ex){
         ex.printStackTrace();
      }
   }

   
     
   }


and then in my gameFrame Class i put this:

1  
2  
3  
4  
5  
private Musik musik1;
   
   public void init() {
      Musik.musik1.play();
   }


But nothing happens, where is the problem? There is no sound playing, neither in the Menu where I select "Play Game" nor in the Game itself.
I got this Code from a Tutorial on Youtube,

I hope some1 can help me!

greez
Offline TotalSpelNerd

Senior Newbie


Exp: 3 years



« Reply #1 - Posted 2013-07-14 18:49:32 »

Do you have a class called Sound?

1. you typed
1  
clip = Applet.newAudioClip(Sound.class.getResource(filename));

with Sound.class. Try typing Musik.class instead since that is the class you are inside.
2. If that doesn't help have you tried printing test in the init method so that you know that it is running?

Hope it helps Tongue
Offline BowtieStranger

Junior Newbie





« Reply #2 - Posted 2013-07-14 23:30:04 »

Ouhw, yea i updated it, but still no sound Sad
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline TotalSpelNerd

Senior Newbie


Exp: 3 years



« Reply #3 - Posted 2013-07-15 08:44:41 »

Okay, 3 questions
1. What is your code right now?
2. Did you see if the init method is actually running?
3. Do you get any errors when you are running the code?
Offline BowtieStranger

Junior Newbie





« Reply #4 - Posted 2013-07-15 09:42:35 »

This is my Sound Class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
package game;

import java.applet.Applet;
import java.applet.AudioClip;

public class Musik {
   
   public static final Musik musik1 = new Musik("/src/game/Sound/Test.wav");
   
   private AudioClip clip;

   public Musik(String filename){
      try{
         clip = Applet.newAudioClip(Musik.class.getResource(filename));
     
      }catch(Exception e){
         e.printStackTrace();
      }
   }
   
   public void play(){
      try{
         new Thread(){
            public void run(){
               clip.play();
            }
         
         }.start();  
         }catch(Exception ex){
         ex.printStackTrace();
      }
   }

   
     
   }


And i tried to put that code:

1  
2  
3  
4  
5  
private Musik musik1;
   
   public void init() {
      Musik.musik1.play();
   }


In my GameFrame, in my MenuFrame on everyplace but nothing happens.


I'm total newb, so how do i put the print in my init method? Thanks for help!

No errors, just nothing is happening

Offline davidc
« Reply #5 - Posted 2013-07-15 09:57:08 »

I assume this is an Applet, are you seeing anything in the Java console?
Offline TotalSpelNerd

Senior Newbie


Exp: 3 years



« Reply #6 - Posted 2013-07-15 10:02:30 »

just do
1  
2  
3  
4  
 public void init() {
      Musik.musik1.play();
      System.out.println("test");
}

If the console prints out test the init method is running and if not you didn't call the method.
Offline kevglass

JGO Kernel


Medals: 191
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2013-07-15 10:11:02 »

Well assuming play() didn't throw an exception of course.

Cheers,

Kev

Offline Stranger

Senior Duke


Medals: 7



« Reply #8 - Posted 2013-07-15 11:16:46 »

Maybe this is the same problem:

]http://stackoverflow.com/questions/8281942/java-audioclip-no-sound-no-error?rq=1]

Anton
Online SHC
« Reply #9 - Posted 2013-07-15 11:54:06 »

Try this. I've had a few modifications for your code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
package game;

import java.applet.Applet;
import java.applet.AudioClip;

public class Musik extends Applet {

    public static final Musik musik1 = new Musik("/src/game/Sound/Test.wav");

    private AudioClip clip;

    public Musik(String filename) {
        try {
           clip = getAudioClip(getDocumentBase(), filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void play() {
        try {
            new Thread() {
                public void run() {
                    clip.play();
                }
            }.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

Try now. I had modified right now and I hadn't tested this. Also
AudioClip
isn't available for applications.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (59 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!