More getResource and Eclipse question
This is kind of an extension from a previous post that I did. The following code I got from KevinWorkman and it deals with loading a wav file as a resource. I took Kevin's code and created a stand alone program to see if it works. I can get it to compile and run without errors but no sound plays. I'm using Eclipse IDE.
I've imported the wav file into the src directory which contains the class that calls the resource. I've also imported it into the root directory of the project itself. I've used a forward slash at the beginning of the file name. I also tried to just load it as a File instead of using getClass().getResource("boxing_bell.wav"); I can't get it to play.
I'm at a complete loss.
Here's the code:
Code Java:
public class playClip {
private Clip music;
private void startSong(){
try{
System.out.println("Starting...\n");
AudioInputStream stream = AudioSystem.getAudioInputStream(
getClass().getResource("boxing_bell.wav"));
// AudioInputStream stream = AudioSystem.getAudioInputStream(new File("boxing_bell.wav"));
music = AudioSystem.getClip();
music.open(stream);
music.start();
System.out.println("Done...\n");
}
catch(Exception e){
e.printStackTrace();
music = null;
}
}
public static void main(String args[])
{
playClip play = new playClip();
play.startSong();
}
}
I also tried making a resource directory within the src directory and including that in the build path.
Any ideas of what I'm doing wrong? I think it has more to do with Eclipse than the code. The reason I say this is I found a book on Safari Online that is almost identical to the code above.
Can you say "Clueless!"
Thanks
Curt
Re: More getResource and Eclipse question
Going over your other thread, you cannot use
getClass().getResource() is used for getting the wav file from within a jar.
Because you have written and compiled this within Eclipse, you need to put the .wav file in the projects root directory and use:
Hopefully that should work.
Re: More getResource and Eclipse question
If you are still having issues, try this code. I've used it in the past and know it works.
Code Java:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineEvent.Type;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class PlayAudio {
private static void playClip(File clipFile) throws IOException,
UnsupportedAudioFileException, LineUnavailableException, InterruptedException {
class AudioListener implements LineListener {
private boolean done = false;
@Override public synchronized void update(LineEvent event) {
Type eventType = event.getType();
if (eventType == Type.STOP || eventType == Type.CLOSE) {
done = true;
notifyAll();
}
}
public synchronized void waitUntilDone() throws InterruptedException {
while (!done) { wait(); }
}
}
AudioListener listener = new AudioListener();
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(clipFile);
try {
Clip clip = AudioSystem.getClip();
clip.addLineListener(listener);
clip.open(audioInputStream);
try {
clip.start();
listener.waitUntilDone();
} finally {
clip.close();
}
} finally {
audioInputStream.close();
}
}
public static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException{
File wav = new File("test.wav");
playClip(wav);
}
}
Make sure the wav file is in the correct location. In this case it's in the project root.
May also be worth looking at - Incredibly Easy Way To Play Sounds
Re: More getResource and Eclipse question
Quote:
Originally Posted by
JavaPF
Because you have written and compiled this within Eclipse, you need to put the .wav file in the projects root directory and use:
Hopefully that should work.
JavaPF,
Thanks for the reply. I tried the above, again, but to no avail. I had it commented out to show that I had tried it, but it still didn't work. I thought it was something I was/wasn't doing in Eclipse but I compiled the code on the command line and it still didn't work, so I'm not sure what is going on.
As far as your 2nd reply, I put the code in and it worked. Thanks for that.
The whole point of this was the code I have is very long and tedious. The code I got from KevinWorkman was very short and compact and much easier to understand.
Thanks for all the help.
Re: More getResource and Eclipse question
To get resources you need to go and get a shovel. You can dig down and collect the rocks and stuff. Thats what i do in minecraft. Thank me if i helped :D