坚持在java中播放wav文件...需要一些指导

时间:2014-11-29 18:04:39

标签: java audio wav

我按下按钮时尝试在java中播放声音。所以我有以下内容:

public void playSound(File soundFile) {
    try {
        AudioInputStream stream = AudioSystem.getAudioInputStream(soundFile);
        AudioFormat format = stream.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(stream);
        clip.start();
    }
    catch (Exception e) {e.printStackTrace();}
}

然后我在buttonListener中调用actionPerformed中的方法:

playSound(new File("woow_x.wav"));

但它正在抛出UnsupportedAudioFileException。这是说不支持.wav文件吗?我确认wav文件有效,所以我不知道问题是什么。而且我试图在不使用太阳的情况下这样做。请告诉我这是如何正确完成的。谢谢。

堆栈追踪:

    javax.sound.sampled.UnsupportedAudioFileException: could not get audio input 
    stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at robotMaze.SystemGUI.playSound(SystemGUI.java:183)
    at robotMaze.SystemGUI$SendButtonListener.actionPerformed(SystemGUI.java:229)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.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$400(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)

1 个答案:

答案 0 :(得分:1)

文件的编码在其标题中指定为0x55。我不确定0x55究竟是什么,但我怀疑它是mp3。无论哪种方式,它都不是Java支持的编码之一。

出于您的目的(如果许可证允许),您可以使用音频编辑器将文件转换为支持的格式。 (这些由AudioFormat.Encoding中的静态字段枚举。)