java.io.IOException异常:标志/复位不支持 [英] 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());
}
这code正常工作,当我运行从NetBeans中的应用。播放声音时,也没有例外。然而,当我从dist文件夹中运行它,声音不玩了,我得到了 java.io.IOException异常:标志/复位不支持
在我的消息对话框
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?
推荐答案
有关文档<一个href=\"http://download.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream%28java.io.InputStream%29\"><$c$c>AudioSystem.getAudioInputStream(InputStream)$c$c>说:
此方法的实现可能
需要多个解析器来检查
的流,以确定它们是否
支持它。这些解析器必须能够
标记流,读取足够多的数据
以确定它们是否支持
流,并且,如果没有,重置
流的阅读指针到原来的
位置。如果输入流不
支持这些操作,这种方法
可能会失败,并抛出IOException。
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.
因此,提供给此方法的流必须支持可选的<一个href=\"http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html#markSupported%28%29\">mark/reset功能。以<一个装饰你的资源流href=\"http://download.oracle.com/javase/6/docs/api/java/io/BufferedInputStream.html\"><$c$c>BufferedInputStream$c$c>.
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);
这篇关于java.io.IOException异常:标志/复位不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!