I guess my code is okay, and my .jar file its okay with the .wav inside it.. But when I try to load it using getResourceAsStream I get a error..
this is my error:
java.io.IOException: mark/reset not supported
at java.util.zip.InflaterInputStream.reset(Unknown Source)
at java.io.FilterInputStream.reset(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unkno
wn Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at operation.MainWindowOperations.prepareAudio(MainWindowOperations.java
:92)
at operation.MainWindowOperations.<init>(MainWindowOperations.java:81)
at graphics.LaunchGraphics.<init>(LaunchGraphics.java:25)
at run.RunApp.main(RunApp.java:14)
this is my code:
private void prepareAudio() {
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav");
inputStream = AudioSystem.getAudioInputStream(is);
clip = AudioSystem.getClip();
clip.open(inputStream);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Can someone help me? thanks alot in advance!!
Java Sound requires repositionable (mark/reset supported) input streams for some operations. If you strike this problem it is because the stream is not repositionable.
One way to get around it is to put the byte[]
of the original stream into a ByteArrayInputStream
, which supports mark/reset.
The 2nd answer on the question linked by Eric R. is also a possibility, and looks simpler. To try it, change..
InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav");
inputStream = AudioSystem.getAudioInputStream(is);
To:
URL url = this.getClass().getClassLoader().getResource("beep.wav");
inputStream = AudioSystem.getAudioInputStream(url);
I got the answer with the help of Tim Moores at JavaRanch. I thought the clearest thing would be to just post it here, even though Andrew mentions this correct answer in the second part of his answer. (The first part works too, but is overkill.)
Url url = this.getClass().getResource("beep.wav");
inputStream = AudioSystem.getAudioInputStream(url);
http://www.coderanch.com/t/558274/Applets/java/mark-reset-not-supported-getResourceAsStream
Tim wrote: I have no specific knowledge about this problem, or audio in applets in general, but I'm not surprised that mark/reset doesn't work with resources obtained through the ClassLoader mechanism. (Actually, I'm sort of surprised it works at all, at least some of the time :-)
Assuming that the audio file is publicly accessible through HTTP, try AudioSystem.getAudioInputStream(URL) instead of the InputStream version you're using now. Looking at the javax.sound.sampled.spi.AudioFileReader javadocs (which is the class being used underneath), only the InputStream variant talks about mark/reset problems, not the URL version.
This also came up here: https://forums.oracle.com/forums/thread.jspa?threadID=2289395&tstart=0 and the answer is near the bottom, along with a Oracle Bug Reference #7095006 which is an interesting read as it explains why the code (as the op originally attempted) used to work but no longer does.
来源:https://stackoverflow.com/questions/9659842/java-exception-reading-stream-from-resource-wav