Java.io.IOException: mark / reset is 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. Sound is reproduced and there are no exceptions. However, when I run it from the dist folder, the sound does not play, and I get java.io.IOException: mark/reset not supported in my message dialog.

How can i fix this?

+51
java audio ioexception
Apr 03 2018-11-11T00:
source share
4 answers

The documentation for AudioSystem.getAudioInputStream(InputStream) says:

Implementing this method may require several parsers to examine the thread to determine if they support it. These parsers should be able to mark the stream, just read the data to determine whether they support the stream, and if not, the reset stream reads a pointer to its original position. If the input stream does not support this operation, this method may fail with an IOException error.

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

 //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); 
+110
Apr 3 2018-11-11T00:
source share

Having floundered several times and linking to this page, I came across this , which helped me with my problem. At first I managed to download the wav file, but subsequently it could be played only once because it could not rewind it due to an error " mark / reset not supported ". It was crazy.

The linked code reads an AudioInputStream from a file, then puts an AudioInputStream in a BufferedInputStream, and then returns it back to an AudioInputStream as follows:

 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 into PCM encoding:

 audioInputStream = convertToPCM(audioInputStream); 

With convertToPCM is 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 someone out there.

+5
Nov 28 '11 at 16:43
source share

Just stumbled upon this question from someone else with the same problem that referred to it.

Oracle Bug Database, # 7095006

Use the following code to avoid the InputStream step. This InputStream is causing an error.

 URL url = AudioMixer.class.getResource(fileName); AudioInputStream ais = AudioSystem.getAudioInputStream(url); 

voila - no InputStream

mark / reset exception during getAudioInputStream ()

+4
Feb 17 '12 at 7:02
source share

The problem is that the input stream must support the method label and reset. At least if the character is supported, you can test with: AudioInputStream # markSupported .

So you should use another InputStream.

0
Apr 03 2018-11-11T00:
source share



All Articles