Sound Problem in my Game

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

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:

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

Do you have a class called Sound?

  1. you typed
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 :stuck_out_tongue:

Ouhw, yea i updated it, but still no sound :frowning:

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?

This is my Sound Class:

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:

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

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

just do

 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.

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

Cheers,

Kev

Maybe this is the same problem:

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

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


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 [icode]AudioClip[/icode] isn’t available for applications.