Results 1 to 8 of 8
  1. #1
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Playing a WAV or MP3

    Time for me to add a little life to my game with some music and sound effects. Unfortunately even after searching about for hours I'm still no closer to accomplishing this. Any tips or hints you guys could give me? A link to a helpful tutorial would be great as well!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default Re: Playing a WAV or MP3

    The Applet class has a static method that gives you a Clip; you can play and stop the Clip. There is no need to worry: the method in the Applet class is a static one so there is no need to build an Applet just for this.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Playing a WAV or MP3

    Thank you so much JosAH! I was able to do some research and made this:

    Java Code:
    import java.applet.*;
    
    public class sound extends Applet
    {
    	AudioClip shoot1;
    	
    	public void sound()
    	{
    	shoot1 = getAudioClip(getCodeBase(), "sound/shoot1.wav");
    	
    	}
    	
    	
    	public void shoot1(int a)
    	{
    		if(a == 0)
    			shoot1.stop();
    		if(a == 1)
    			shoot1.play();
    	}
    	
    }
    However, it appears as if it isn't able to access the code (Eclipse underlined public class sound and public void sound in yellow)
    and I'm getting one of those nasty nullPointerException errors.

    I'm trying to access them in my initializer method with this:

    Java Code:
     //Load sound
            s = new sound();
            s.sound();
    Also, how would you play the sound only once? It doesn't appear to be rhetorical, to me at least.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Playing a WAV or MP3

    What does the sound class (a better name would be Sound) represent? If it is not an applet then it should not extend Applet.

    Eclipse underlined public class sound and public void sound in yellow
    Eclipse is probably warning you that you are using the same name for the class and the method. You can hover over the little yellow warning triangle and read the actual warning message.

    I'm getting one of those nasty nullPointerException errors.
    If you have a runtime exception, post the actual output (the stacktrace) and the code that is giving rise to it.

    -----

    To play the sound you will need to call the shoot1() method.

  5. #5
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Playing a WAV or MP3

    Yeah that would most likely be why it was underlining it in retrospect...

    Here is the stacktrace:

    java.lang.NullPointerException
    at java.applet.Applet.getCodeBase(Unknown Source)
    at sound.<init>(sound.java:5)
    at game.init(game.java:94)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


    line 5 of class sound:
    Java Code:
    AudioClip shoot1 = getAudioClip(getCodeBase(), "sound/shoot1.wav");
    line 94 of class game:
    Java Code:
    s = new sound();
    I get the same error when calling shoot1(), I forgot to mention that.
    I had to have class sound extend Applet in order for it to recognize the getCodeBase() I used to load the sound, I kind of thought that wasn't how you did it... :(

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Playing a WAV or MP3

    java.lang.NullPointerException
    at java.applet.Applet.getCodeBase(Unknown Source)
    at sound.<init>(sound.java:5)
    at game.init(game.java:94)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    The rule of thumb with stack traces is to read them from the top until you hit a line that refers to your code. This is the place that things are going wrong.

    So the culprit is sound.<init> which is line 5 of sound.java. The line number is a bit deceptive; what the <init> means is that the exception occurs as the sound class is being initialised. In particular it will call the super class constructor (ie the Applet constructor). Applets are a bit strange as they need a "context" in which to run. This is (usually) provided by a web browser and it will tell the java runtime things like the code base. None of that will be able to happen here, because yours is not a "proper" applet running in a proper context.

    The solution is what I said before: the class should not extend Applet.

    -----

    You call the getAudioClip() method as you would any other static method:

    Java Code:
    AudioClip clip = Applet.getAudioClip(/*...*/);
    The argument you give the method will be a URL describing where your sound file is. Note you can construct a URL from an instance of File. (See the API docs)

    Quite possibly you don't need a separate class to work with the sound clip. Just use Applet.getAudioClip() whereever you want a sound clip, and do whatever you like with that clip.

  7. #7
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Playing a WAV or MP3

    Quite possibly you don't need a separate class to work with the sound clip. Just use Applet.getAudioClip() whereever you want a sound clip, and do whatever you like with that clip.
    I tried creating the clips in my game class and it works fine! I guess I didn't even need to make another class after all >_<
    Thank you everyone, you guys are so helpful!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Playing a WAV or MP3

    You're welcome. Good luck with the game.

Similar Threads

  1. Playing . MP3 in JAVA
    By UJJAL DHAR in forum Advanced Java
    Replies: 1
    Last Post: 12-12-2011, 03:54 PM
  2. sound playing
    By nikkka in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-10-2011, 05:38 PM
  3. Wav player not playing
    By RightGoodEnt in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-13-2011, 04:23 PM
  4. Playing an Animated GIF
    By c0m4ndo45 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2009, 10:47 AM
  5. playing audio in J-App??
    By ashton in forum New To Java
    Replies: 3
    Last Post: 01-30-2009, 08:50 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •