2

I've been trying to play a .wav file in my java project and I got it working....or so I thought. When I exported it to an executable jar file, the sounds won't play unless they're in the same directory as the jar file. I want it to load the sounds that a inside of the jar file. Here's the code I'm using right now:

    void PlaySound(String filename) {
        try (InputStream in = getClass().getResourceAsStream(filename)) {
            try (AudioInputStream audioIn = AudioSystem.getAudioInputStream(in)) {
                Clip clip = AudioSystem.getClip();
                clip.open(audioIn);
                clip.start();
            }

        } catch (Exception e) {
           e.printStackTrace();
       }
    }
MyLegGuy
  • 95
  • 6
  • What is the path to the wave file (within the Jar File) and what are you using to try and load it? – MadProgrammer Jul 09 '15 at 01:31
  • It sounds like the wave files aren't been embedded within the Jar file. How are you creating the jar file? Where are the wave files located with relation to your source/project? – MadProgrammer Jul 09 '15 at 01:32
  • What do you mean, what am I using to try and load it? – MyLegGuy Jul 09 '15 at 02:00
  • What path `String` are you using (the value of, like `/path/to/my/awesome/sounds/...`)? – MadProgrammer Jul 09 '15 at 02:02
  • Wasn't ready for that comment, wanted to make a line break. In the jar file, my sounds are located in a folder called nathan. That's my package name. Forgot to mention, this method for playing sounds works perfectly, but not when I export it. – MyLegGuy Jul 09 '15 at 02:03
  • Just a slash before the file name. Like this: /mysound.wav. The class file playing the sound and the sounds are in the same package. – MyLegGuy Jul 09 '15 at 02:03
  • So, have you tried using `/nathen/{name of wave file}`? Did you also check the Jar file contents to make sure that the wave files were included and included where you thought they should be? (Jar files are just zip files) – MadProgrammer Jul 09 '15 at 02:05
  • I did check that the wav files are in there. I did so using winrar. They're in my package. I tried putting the path like how you told me (/nathan/mysound.wav) and it still didn't work when I exported the file. Still worked fine before exporting. – MyLegGuy Jul 09 '15 at 02:08
  • Are you getting any exceptions? What's path to the wave files from the root of the Jar file? – MadProgrammer Jul 09 '15 at 02:11
  • How do I tell if I'm getting an exception? Path from the root of the jar file is /nathan/mysound.wav – MyLegGuy Jul 09 '15 at 02:19
  • Run the program from the command line (`java -jar {your jar name}`) and see if anything is printed to the console... – MadProgrammer Jul 09 '15 at 02:40
  • I tried that and here's the error I got: java.io.IOException: mark/reset not supported at java.util.zip.InflaterInputStream.reset(Unknown Source) at java.io.FilterInputStream.reset(Unknown Source) at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unkno wn Source) at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) at nathan.MainClass.PlaySound(MainClass.java:581) at nathan.MainClass.(MainClass.java:88) at nathan.MainClass.main(MainClass.java:74) – MyLegGuy Jul 09 '15 at 02:50
  • I found this question: http://stackoverflow.com/questions/5529754/java-io-ioexception-mark-reset-not-supported and it fixed my problem! I'm going to answer my own question and then accept it as the answer! – MyLegGuy Jul 09 '15 at 02:56

1 Answers1

3

I needed to change it to:

void PlaySound(String filename) {
        try (InputStream in = getClass().getResourceAsStream(filename)) {
            InputStream bufferedIn = new BufferedInputStream(in);
            try (AudioInputStream audioIn = AudioSystem.getAudioInputStream(bufferedIn)) {
                Clip clip = AudioSystem.getClip();
                clip.open(audioIn);
                clip.start();
            }
        } catch (Exception e) {
           e.printStackTrace();
       }
    }

But I actually got this solution from here: java.io.IOException: mark/reset not supported

Community
  • 1
  • 1
MyLegGuy
  • 95
  • 6