简体   繁体   English

在jar中播放WAV文件-Java NetBeans

[英]play wav file in jar - java netbeans

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 : 我有一个用于在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)

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. 在netbeans中,我将music.wav文件放在src项目文件夹中,并在另一台试图运行jar文件的机器上,我将music.wav放在jar内和旁边。

I wonder what is the exception is about? 我想知道例外是什么?

Any Help? 有帮助吗?

I downloaded your audio file: it's 16-bit stereo PCM, but has the unusual sample rate of 11kHz. 我下载了您的音频文件:它是16位立体声PCM,但具有11kHz的异常采样率。 Most wav playbacks I do are 44kHz (note, values are rounded in Windows Properties display). 我执行的大多数wav播放都是44kHz(注意,值在“ Windows属性”显示中取整)。 Maybe this format is supported in some IDEs but not others? 也许某些IDE支持这种格式,但其他IDE不支持吗? or some JVM's but not others? 或某些JVM而不是其他? You can try converting it to 44kHz with Audacity (free software DAW) and see if it then runs. 您可以尝试使用Audacity(免费软件DAW)将其转换为44kHz,然后查看它是否可以运行。 Or, find another 44kHz wav and test it using the same code. 或者,找到另一个44kHz wav,并使用相同的代码对其进行测试。

Since you are using URLs, they should work for resources stored within a jar. 由于您使用的是URL,因此它们应适用于jar中存储的资源。 I tend to use the following to get audio: 我倾向于使用以下内容获取音频:

1) create an "audio" subfolder. 1)创建一个“音频”子文件夹。 Thus if your code is in the package com.zaid.game, the wav files would be at ..../com/zaid/game/audio 因此,如果您的代码位于com.zaid.game包中,则wav文件位于.... / com / zaid / game / audio

2) when calling the file name, add the subfolder to the string: 2)调用文件名时,将子文件夹添加到字符串中:

fileName = "audio/" + fileName;

3) use the following to set the URL variable: 3)使用以下设置URL变量:

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

and then 接着

AudioInputStream aiStream = AudioSystem.getAudioInputStream(url);

as you do. 像你一样做。

This assumes that "this.getClass()" is in the package above the 'audio' folder. 假定“ this.getClass()”位于“ audio”文件夹上方的包中。

PS, I'm not clear what the 'getClassLoader()' does for you in the URL assignment. PS,我不清楚在URL分配中'getClassLoader()'为您做什么。 I don't use it, and am not familiar with it. 我不使用它,也不熟悉它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM