gpt4 book ai didi

java - 为什么 AudioSystem.getAudioInputStream() 会抛出 IllegalArgumentException

转载 作者:行者123 更新时间:2023-12-02 22:40:15 25 4
gpt4 key购买 nike

我正在 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)

我对此很困惑,我只想播放一个声音! ;(如果你们中的一个可以帮助我,我将非常感激!

最佳答案

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

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

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

关于java - 为什么 AudioSystem.getAudioInputStream() 会抛出 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27476225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com