繁体   English   中英

无法从输入流创建音频流

[英]Cannot Create Audio Stream from Input Stream

我试图单击一个按钮时播放声音文件(.wav)。 呼叫均正确进行。 当我单击按钮时,将打印IOException。 这是开始音乐的方法的代码:

public void playSound(String path) {

//M:\Programming\workspace\testing\music.wav  <--- String path
    InputStream in;
    AudioStream as = null;

    try {
        in = new FileInputStream(path);
        as = new AudioStream(in);
    } catch (FileNotFoundException e) {
        System.out.println("Audio file not found.");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Incorrect input.");
        e.printStackTrace();
    }
    AudioPlayer.player.start(as);
}

这是错误:

java.io.IOException: could not create audio stream from input stream
at sun.audio.AudioStream.<init>(Unknown Source)
at GameWindow.playSound(GameWindow.java:484)
at GameWindow$13.mouseReleased(GameWindow.java:385)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我尝试了许多方法来播放在Google上找到的音频文件,但是所有方法都返回相同的错误。 我需要做什么来解决这个问题? 如果有更好的方法来处理音频文件,您是否可以提供任何资源来查看?

尝试实例化WaveFileReader (包com.sun.media.sound )-这应该用于读取文件。 如果收到UnsupportedAudioFileException则可能是您的wav文件损坏-请尝试使用其他文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM