导致异常的代码:
AudioInputStream aisStream =
AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(strPath));
官方文档关于 AudioSystem.getAudioInputStream(InputStream)的说明:
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.
也就是说,为这个方法提供的流必须支持mark/reset (标记/重置)功能,解决方法是用BufferedInputStream把原来的流包一层,修改后的代码如下:
InputStream audioSrc= Sound.class.getResourceAsStream(strPath);
InputStream bufferedIn=new BufferedInputStream(audioSrc);
AudioInputStream aisStream =
AudioSystem.getAudioInputStream(bufferedIn);
参考:http://www.itkeyword.com/doc/0860229578511812x277/java-io-ioexception-mark-reset-not-supported