问题
I am creating a software for personal use titled BatteryBeeper. It will remind me to charge the laptop when the charge is whatever I set to be reminded at.
It is supposed to play a sound when the charge hits the set threshold.
I had a look at the answer: sound not playing in jar and my AudioInputStream
is constructed similar to what Jigar Joshi mentioned.
However I get a null exception:
java.lang.NullPointerException
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at demo.BatteryBeeper.setupSoundPlayback(BatteryBeeper.java:169)
at demo.BatteryBeeper.<init>(BatteryBeeper.java:41)
at demo.BatteryBeeper.main(BatteryBeeper.java:35)
Here is my code to load the sound:
public void setupSoundPlayback(){
try{
buzzer = AudioSystem.getClip();
in = AudioSystem.getAudioInputStream(BatteryBeeper.class.
getResourceAsStream("sound/buzzer3_x.wav"));
buzzer.open(in);
}catch(Exception e){
e.printStackTrace();
}
}
Here is the code to play it:
public void playSound(){
buzzer.start();
}
What is causing the problem ?
Misc
This is a usual Eclipse project. There is a sound
folder under src
which has the wave file
Edit
After AlexR's answer, I changed the path and got a new exception:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at demo.BatteryBeeper.setupSoundPlayback(BatteryBeeper.java:169)
at demo.BatteryBeeper.<init>(BatteryBeeper.java:41)
at demo.BatteryBeeper.main(BatteryBeeper.java:35)
回答1:
You problem is not in playing the sound. Your problem is in access to embedded resource. When you are using BatteryBeeper.class.getResourceAsStream("sound/buzzer3_x.wav")
you try to retrieve resource located in same package where class BatteryBeeper
is. This means that your file is in /demo/sound/buzzer3_x.wav
. It seems this is wrong. If your file really is in sound/buzzer3_x.wav
use getResourceAsStream("/sound/buzzer3_x.wav")
(pay attention on the leading slash in the path).
回答2:
If you are still getting the message "could not get AudioInputStream from InputStream" the problem is most likely with the InputStream stage not throwing an informative error.
If you look at the specs for AudioSystem.getAudioInputStream(InputStream) you will see that the input stream may fail due to Mark/Reset action not being supported, but for some reason, it chooses to throw other error messages!!
http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream%28java.io.InputStream%29
Better, use the form AudioSystem.getAudioInputStream(URL) and the markability of your audio file is not an issue, because an intermediate InputStream is never created afaik.
http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream%28java.net.URL%29
The URL form has the additional benefit of working well with files embedded in a Jar. Of course, you still have to provide the correct address.
来源:https://stackoverflow.com/questions/14375338/cannot-play-embedded-sound-in-a-jar-file