JAR file does not play sound until Eclipse

I have a project that I want to export as a jar file. The software must play sound at a specific point in time. However, the problem is that the JAR file does not play the sound file when this point in time is reached. When I run the same project from the Eclipse IDE it does this.

Here's the code that shows how I set up the audio playback capabilities:

public void setupSoundPlayback(){
        try{
            buzzer = AudioSystem.getClip();

            in = AudioSystem.getAudioInputStream(BatteryBeeper.class.
                    getResourceAsStream("/sound/boing_x.wav"));

            buzzer.open(in);
        }catch(Exception e){
            e.printStackTrace();
        }

    }  

      

Later I call the method of the play()

object Clip

. Although I am using the method getResourceAsStream()

to play embedded audio, the JAR does not play audio.



enter image description here

As I was told to extract and see if the sound file was in place, I did it. Here's what I got. The audio folder is at the root. And boing3_x.wav

located in the sound folder (not in the picture).

+3


source to share


3 answers


Since you are using a call getResourceAsStream("/sound/boing_x.wav");

, you must make sure the .wav file is wrapped in the JAR in the correct location.

Since you have a "/" at the beginning of the path, the .wav file must be located inside the JAR at the path you specify using the absolute path (i.e. the sound directory must be located at the root of the JAR and boing_x.wav must be located in the sound directory ).



I would recommend unzipping your JAR file to a specific directory and making sure that when you unzip the JAR you see /sound/boing_x.wav

.

+3


source


This happens when the resource, in your case a .wav file, is not embedded in an embedded .jar file or is embedded in the wrong place.



Also note that you are trying to play "/ sound / boing_x.wav ", but you also note that in the bank, in / sound, you have boing 3 _x.wav , so the filename has an extra 3 in its name.

+2


source


This is my function to play a loop sound file in banks, it works great for me.

It seems that getResourceAsStream()

does not work with the banks. however getResource()

does.

public synchronized void alarm() {
    try {
        crit = AudioSystem.getClip();
        AudioInputStream inputStream1 = AudioSystem.getAudioInputStream(this.getClass().getResource("critical.wav"));
        crit.open(inputStream1);
        crit.loop(Clip.LOOP_CONTINUOUSLY);

    } catch (Exception e) {
        System.err.println(e.getMessage());
        }
}

      

+1


source







All Articles