Java Exception Read Stream from .wav resource

I think my code is fine and my .jar file is fine with the WAV inside it. But when I try to load it using getResourceAsStream I get an error.

this is my mistake:

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 anyone help me? thanks alot in advance!

+3


source to share


2 answers


Java Sound requires permutation (mark / reset supported) input streams for some operations. If you face this problem, it is because the stream cannot be changed.

One way to get around this is to put the byte[]

original stream into ByteArrayInputStream

one that supports mark / reset.


The second answer to the question related to Eric R. is also possible and looks simpler. To try, 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);

      

+6


source


I got the answer with the help of Tim Moors at JavaRanch. I thought the simplest thing would be to just post it here, although Andrey mentions this correct answer in the second part of his answer. (The first part works too, but is overwhelming.)

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 does not work with resources obtained using the ClassLoader mechanism. (Actually, I kind of surprised that it works at all, at least for a while :-)

Assuming the audio file is publicly available over HTTP, try AudioSystem.getAudioInputStream (URL) instead of the InputStream you are currently using. Looking at the javax.sound.sampled.spi.AudioFileReader javadocs (which is the class used underneath), only the InputStream variant says mark / reset, not the URL version.

This also popped up here: https://forums.oracle.com/forums/thread.jspa?threadID=2289395&tstart=0 and the answer is near the bottom, as well as a link to Oracle Bug Reference # 7095006 which is interesting to read as it explains why the code (as it was originally executed) was used but no longer works.

+1


source







All Articles