user528050
user528050

Reputation: 157

Got an exception while working with javax.sound.samples library....!

Can anyone tell me where i have done mistake in this java program. I always get an exception caught while i run this.

import java.io.*;
import javax.sound.sampled.*;
public class x 
{
 public static void main(String args[])
 {
  try
  {
  File f=new File("mm.wav");
  AudioInputStream a=AudioSystem.getAudioInputStream(new FileInputStream(f));
  AudioFormat audioFormat = a
                   .getFormat();
             DataLine.Info dataLineInfo = new DataLine.Info(
                   Clip.class, audioFormat);
             Clip clip = (Clip) AudioSystem
                   .getLine(dataLineInfo);
             clip.open(a);
             clip.start();
 }
 catch(Exception e)
 {
  e.printStackTrace();
  System.out.println("exception caught ");
 }
 }
}

It will throws this exception

java.io.IOException: mark/reset not supported
    at java.io.InputStream.reset(InputStream.java:351)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:135)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1111)
    at x.main(x.java:10)

Upvotes: 1

Views: 1504

Answers (2)

Heinrich Tegethoff
Heinrich Tegethoff

Reputation: 21

Stumbled over the same issue, and dived into javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1113) The code there asks providers whether they can handle the sound format, and catches UnsupportedAudioFileException to continue with next provider. Code comments state "throws IOException", and IOException happens when AudioSystem tries the provider com.sun.media.sound.SoftMidiAudioFileReader Yes, the provider throws UnsupportedAudioFileException, but before doing so it calls "reset()" on the input stream. I use package resources via standard URL.openStream(), the URL stream doesn't support reset(), and throws IOException just before SoftMidiAudioFileReader throws UnsupportedAudioFileException.

Bug in class AudioSystem: UnsupportedAudioFileException is catched, and continues with next provider, while the unnecessary IOException aborts all other providers.

Your stacktrace shows you stepped into the same bug. Got no work around yet.

Upvotes: 2

robert_x44
robert_x44

Reputation: 9314

EDITED:

After testing your code, I get the same error every time I try to give your code an invalid sound file (I gave it text files and binary java class files :) ). I got the same exception every time. It's not a very user-friendly exception, but check the validity of your wav file.

EDIT #2:

If I change the code to:

  AudioInputStream a=AudioSystem.getAudioInputStream(
      new BufferedInputStream(new FileInputStream(f)));

then an invalid sound file will give the more pleasing exception: UnsupportedAudioFileException

Upvotes: 1

Related Questions