Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.IOException: mark/reset not supported

try {
    //String location = dir1.getCanonicalPath()+"\\app_yamb_test1\\mySound.au";
    //displayMessage(location);
    AudioInputStream audio2 = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("mySound.au"));
    Clip clip2 = AudioSystem.getClip();
    clip2.open(audio2);
    clip2.start();
} catch (UnsupportedAudioFileException uae) {
    System.out.println(uae);
    JOptionPane.showMessageDialog(null, uae.toString());
} catch (IOException ioe) {
    System.out.println("Couldn't find it");
    JOptionPane.showMessageDialog(null, ioe.toString());
} catch (LineUnavailableException lua) {
    System.out.println(lua);
    JOptionPane.showMessageDialog(null, lua.toString());
}

This code works fine when I run the application from netbeans. The sound plays and there are no exceptions. However, when I run it from the dist folder, the sound does not play and I get the java.io.IOException: mark/reset not supported in my message dialog.

How can I fix this?

like image 597
Crais Avatar asked Apr 03 '11 13:04

Crais


3 Answers

The documentation for AudioSystem.getAudioInputStream(InputStream) says:

The implementation of this method may require multiple parsers to examine the stream to determine whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream's read pointer to its original position. If the input stream does not support these operation, this method may fail with an IOException.

Therefore, the stream you provide to this method must support the optional mark/reset functionality. Decorate your resource stream with a BufferedInputStream.

//read audio data from whatever source (file/classloader/etc.) InputStream audioSrc = getClass().getResourceAsStream("mySound.au"); //add buffer for mark/reset support InputStream bufferedIn = new BufferedInputStream(audioSrc); AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn); 
like image 56
McDowell Avatar answered Oct 23 '22 14:10

McDowell


After floundering about for a while and referencing this page many times, I stumbled across this which helped me with my problem. I was initially able to load a wav file, but subsequently only could play it once, because it could not rewind it due to the "mark/reset not supported" error. It was maddening.

The linked code reads an AudioInputStream from a file, then puts the AudioInputStream into a BufferedInputStream, then puts that back into the AudioInputStream like so:

audioInputStream = AudioSystem.getAudioInputStream(new File(filename)); BufferedInputStream bufferedInputStream = new BufferedInputStream(audioInputStream); audioInputStream = new AudioInputStream(bufferedInputStream, audioInputStream.getFormat(), audioInputStream.getFrameLength()); 

And then finally it converts the read data to a PCM encoding:

audioInputStream = convertToPCM(audioInputStream); 

With convertToPCM defined as:

private static AudioInputStream convertToPCM(AudioInputStream audioInputStream)     {         AudioFormat m_format = audioInputStream.getFormat();          if ((m_format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) &&             (m_format.getEncoding() != AudioFormat.Encoding.PCM_UNSIGNED))         {             AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,                 m_format.getSampleRate(), 16,                 m_format.getChannels(), m_format.getChannels() * 2,                 m_format.getSampleRate(), m_format.isBigEndian());             audioInputStream = AudioSystem.getAudioInputStream(targetFormat, audioInputStream);     }      return audioInputStream; } 

I believe they do this because BufferedInputStream handles mark/reset better than audioInputStream. Hope this helps somebody out there.

like image 40
AndyG Avatar answered Oct 23 '22 15:10

AndyG


Just came across this question from someone else with the same problem who referenced it. Looks like this issue arose with Java 7.

Oracle Bug database, #7095006

A test, executed when InputStream is the argument to the getAudioInputStream() method, is triggering the error. The existence of Mark/Reset capabilities in the audio resource file have no bearing on whether the Clip will load and play. Given that, there is no reason to prefer an InputStream as the argument, when a URL or File suffice.

If we substitute a URL as the argument, this needless test is not executed. Revising the OP code:

AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResource(fileName));

Details can be seen in the API, in the description text for the two forms. AudioSystem.getAudioInputStream(InputStream)

AudioSystem.getAudioInputStream(URL)

like image 25
Phil Freihofner Avatar answered Oct 23 '22 15:10

Phil Freihofner