0

I have got a code for playing wav sound file in java and works perfectly in netbeans IDE but, when i export the project to jar file and run it on another machine, it won't work and show the following exception :

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 quiz.Login.jButton3ActionPerformed(Login.java:178)
at quiz.Login.access$200(Login.java:36)
at quiz.Login$3.actionPerformed(Login.java:96)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
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:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
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:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
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)

my code is :

try {
 // Open an audio input stream.
 URL url = this.getClass().getClassLoader().getResource("music.wav");
 AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
 // Get a sound clip resource.
 Clip clip = AudioSystem.getClip();
 // Open audio clip and load samples from the audio input stream.
 clip.open(audioIn);
 clip.start();
 } catch (UnsupportedAudioFileException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 } catch (LineUnavailableException e) {
 e.printStackTrace();
}

In netbeans I put the music.wav file in src project folder and on another machines where i try to run jar file I put music.wav inside the jar and beside it.

I wonder what is the exception is about?

Any Help?

4

1 回答 1

1

我下载了你的音频文件:它是 16 位立体声 PCM,但具有不寻常的 11kHz 采样率。我做的大多数 wav 播放都是 44kHz(注意,值在 Windows 属性显示中四舍五入)。也许某些 IDE 支持这种格式,但其他 IDE 不支持?或者一些JVM而不是其他的?您可以尝试使用 Audacity(免费软件 DAW)将其转换为 44kHz,然后查看它是否运行。或者,找到另一个 44kHz wav 并使用相同的代码对其进行测试。

由于您使用的是 URL,因此它们应该适用于存储在 jar 中的资源。我倾向于使用以下内容来获取音频:

1)创建一个“音频”子文件夹。因此,如果您的代码位于 com.zaid.game 包中,则 wav 文件将位于 ..../com/zaid/game/audio

2)调用文件名时,将子文件夹添加到字符串中:

fileName = "audio/" + fileName;

3)使用以下设置URL变量:

URL url = this.getClass().getResource(fileName);

接着

AudioInputStream aiStream = AudioSystem.getAudioInputStream(url);

像你一样做。

这假定“this.getClass()”位于“audio”文件夹上方的包中。

PS,我不清楚“getClassLoader()”在 URL 分配中为您做了什么。我没用过,也不熟悉。

于 2013-04-15T20:15:59.797 回答