Results 1 to 8 of 8
Thread: Playing a WAV or MP3
- 12-18-2011, 05:10 PM #1
- 12-18-2011, 05:17 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-18-2011, 06:01 PM #3
Re: Playing a WAV or MP3
Thank you so much JosAH! I was able to do some research and made this:
However, it appears as if it isn't able to access the code (Eclipse underlined public class sound and public void sound in yellow)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(); } }
and I'm getting one of those nasty nullPointerException errors.
I'm trying to access them in my initializer method with this:
Also, how would you play the sound only once? It doesn't appear to be rhetorical, to me at least.Java Code://Load sound s = new sound(); s.sound();
- 12-18-2011, 08:00 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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 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.Eclipse underlined public class sound and public void sound in yellow
If you have a runtime exception, post the actual output (the stacktrace) and the code that is giving rise to it.I'm getting one of those nasty nullPointerException errors.
-----
To play the sound you will need to call the shoot1() method.
- 12-18-2011, 11:40 PM #5
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:
line 94 of class game:Java Code:AudioClip shoot1 = getAudioClip(getCodeBase(), "sound/shoot1.wav");
I get the same error when calling shoot1(), I forgot to mention that.Java Code:s = new sound();
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... :(
- 12-19-2011, 12:37 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Playing a WAV or MP3
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.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)
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:
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)Java Code:AudioClip clip = Applet.getAudioClip(/*...*/);
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.
- 12-19-2011, 02:55 AM #7
Re: Playing a WAV or MP3
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 >_<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.
Thank you everyone, you guys are so helpful!
- 12-19-2011, 02:59 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Similar Threads
-
Playing . MP3 in JAVA
By UJJAL DHAR in forum Advanced JavaReplies: 1Last Post: 12-12-2011, 03:54 PM -
sound playing
By nikkka in forum Threads and SynchronizationReplies: 1Last Post: 04-10-2011, 05:38 PM -
Wav player not playing
By RightGoodEnt in forum CLDC and MIDPReplies: 0Last Post: 02-13-2011, 04:23 PM -
Playing an Animated GIF
By c0m4ndo45 in forum Java 2DReplies: 3Last Post: 04-24-2009, 10:47 AM -
playing audio in J-App??
By ashton in forum New To JavaReplies: 3Last Post: 01-30-2009, 08:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks