简体   繁体   中英

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 :

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?

I downloaded your audio file: it's 16-bit stereo PCM, but has the unusual sample rate of 11kHz. Most wav playbacks I do are 44kHz (note, values are rounded in Windows Properties display). Maybe this format is supported in some IDEs but not others? or some JVM's but not others? You can try converting it to 44kHz with Audacity (free software DAW) and see if it then runs. Or, find another 44kHz wav and test it using the same code.

Since you are using URLs, they should work for resources stored within a jar. I tend to use the following to get audio:

1) create an "audio" subfolder. Thus if your code is in the package com.zaid.game, the wav files would be at ..../com/zaid/game/audio

2) when calling the file name, add the subfolder to the string:

fileName = "audio/" + fileName;

3) use the following to set the URL variable:

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.

PS, I'm not clear what the 'getClassLoader()' does for you in the URL assignment. I don't use it, and am not familiar with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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