Lewd Nitrogen
Lewd Nitrogen

Reputation: 3

AudioClip is not working, and it should

I have been trying to play a sound in java and I am stuck.

public class Audioapp extends JApplet
{

    public class Sound{
        public AudioClip song;
        public URL songPath;
        Sound(){

            try{

               songPath = new URL(getCodeBase(), "leftfordeath.wav"); 

               song = Applet.newAudioClip(songPath); 

            }
            catch (Exception e) {}

        }
        public void playSoundOnce(){
            song.play();
        }
    }

    public void init(){
       Sound test = new Sound();
       test.playSoundOnce();
    }

}

When I call init from my main, it says there is a nullPointerExeption on song.play(); what am I doing wrong, please help...

Upvotes: 0

Views: 79

Answers (1)

Rachel Gallen
Rachel Gallen

Reputation: 28573

try substituting this code where you have the playsoundOnce code and see how you get on.

public void playSoundOnce() 
{
  try
  {
    InputStream inputStream = getClass().getResourceAsStream(leftfordeath.wav);
    AudioStream audioStream = new AudioStream(inputStream);
    AudioPlayer.player.start(audioStream);
  }
  catch (Exception e)
  {
    if (debugFileWriter!=null) e.printStackTrace(debugFileWriter);
  }
}

Upvotes: 1

Related Questions