在jar中播放wav文件 - java netbeans

时间:2013-04-14 16:29:29

标签: java file audio wav javasound

我有一个在java中播放wav声音文件的代码,并在netbeans IDE中完美运行但是, 当我将项目导出到jar文件并在另一台机器上运行时,它将无法工作并显示以下异常:

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)

我的代码是:

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();
}

在netbeans中,我把music.wav文件放在src项目文件夹中,在另一台机器上,我尝试运行jar文件,把music.wav放在jar里面,然后放在它旁边。

我想知道什么是例外?

任何帮助?

1 个答案:

答案 0 :(得分:1)

我下载了你的音频文件:它是16位立体声PCM,但具有11kHz的异常采样率。我做的大多数wav播放是44kHz(注意,值在Windows属性显示中舍入)。也许某些IDE支持这种格式,但其他IDE不支持?或者一些JVM而不是其他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'文件夹上方的包中。

P.S。,我不清楚'getClassLoader()'在URL分配中为你做了什么。我不使用它,我不熟悉它。