来自资源.wav的Java异常读取流 [英] Java Exception Reading Stream from Resource .wav

查看:107
本文介绍了来自资源.wav的Java异常读取流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的代码还可以,我的.jar文件也可以包含.wav文件。.
但是当我尝试使用getResourceAsStream加载它时,我得到了一个错误。

I guess my code is okay, and my .jar file its okay with the .wav inside it.. But when I try to load it using getResourceAsStream I get a error..

这是我的错误:

java.io.IOException: mark/reset not supported
    at java.util.zip.InflaterInputStream.reset(Unknown Source)
    at java.io.FilterInputStream.reset(Unknown Source)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unkno
wn Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at operation.MainWindowOperations.prepareAudio(MainWindowOperations.java
:92)
    at operation.MainWindowOperations.<init>(MainWindowOperations.java:81)
    at graphics.LaunchGraphics.<init>(LaunchGraphics.java:25)
    at run.RunApp.main(RunApp.java:14)

这是我的代码:

private void prepareAudio() {
    try {

        InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav");
        inputStream = AudioSystem.getAudioInputStream(is);
        clip = AudioSystem.getClip();
        clip.open(inputStream);

    } catch (Exception ex) {
        ex.printStackTrace();

    }

}

有人可以帮忙吗我?

推荐答案

Java声音需要可重定位(支持标记/重置)的输入流才能进行某些操作。如果遇到此问题,那是因为流不可重定位。

Java Sound requires repositionable (mark/reset supported) input streams for some operations. If you strike this problem it is because the stream is not repositionable.

解决该问题的一种方法是放置 byte [] 转换为 ByteArrayInputStream ,它支持标记/重置。

One way to get around it is to put the byte[] of the original stream into a ByteArrayInputStream, which supports mark/reset.

由Eric R.链接的问题的第二个答案也是可能的,并且看起来更简单。要尝试它,请更改。.

The 2nd answer on the question linked by Eric R. is also a possibility, and looks simpler. To try it, change..

InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav");
inputStream = AudioSystem.getAudioInputStream(is);

收件人:

URL url = this.getClass().getClassLoader().getResource("beep.wav");
inputStream = AudioSystem.getAudioInputStream(url);

这篇关于来自资源.wav的Java异常读取流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆