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: mark/reset not supported
    at Source)
    at Source)
wn Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at operation.MainWindowOperations.prepareAudio(
    at operation.MainWindowOperations.<init>(
    at graphics.LaunchGraphics.<init>(
    at run.RunApp.main(


this is my code:

private void prepareAudio() {
    try {

        InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav");
        inputStream = AudioSystem.getAudioInputStream(is);
        clip = AudioSystem.getClip();;

    } catch (Exception ex) {




Can anyone help me? thanks alot in advance!


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);



URL url = this.getClass().getClassLoader().getResource("beep.wav");
inputStream = AudioSystem.getAudioInputStream(url);




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);

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: 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.



All Articles