开发者

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

开发者 https://www.devze.com 2023-02-01 10:51 出处:网络
Can anyone tell me where i have done mistake in this java program. I always get an exception caught while i run this.

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(data开发者_如何学PythonLineInfo);
             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)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消