为什么AudioSystem.getAudioInputStream()抛出IllegalArgumentException

时间:2014-12-15 01:33:20

标签: java audio wav illegalargumentexception

我正在Eclipse中开展游戏。我有一些播放wav文件的代码:

public void play(String path) {
    try {
        Clip clip = AudioSystem.getClip();
        URL soundURL = getClass().getResource(path);
        clip.open(AudioSystem.getAudioInputStream(soundURL));
        clip.play();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

问题是,即使我有一个有效的路径(我已经测试过),我收到的错误是:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid format

这里是完整的错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid format
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
at tools.SoundTools.play(SoundTools.java:17)
at gamestate.MainState.mouseReleased(MainState.java:102)
at gamestate.GameStateManager.mouseReleased(GameStateManager.java:56)
at main.GamePanel.mouseReleased(GamePanel.java:118)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我对此非常困惑,我只是想发出声音! ;(如果你们中的一个人能帮助我,我会非常感激!

1 个答案:

答案 0 :(得分:0)

Java显然在抱怨格式,这很可能是.wav文件的AudioFormat。该文件可能以未在java中实现的方式进行压缩,因此无效。 .wav文件通常包含原始PCM数据,但也可以以各种方式压缩它们。

尝试使用音频转换器/编辑器(Audacity)将.wav文件的格式更改为PCM

支持的格式列表:  JMF 2.1.1 - Supported Formats