2

I have been learning about using sound in applications, and everything was working fine. But when I exported the project to an executable .jar everything went to hell...

Basically when the application loads, the window opens but it's blank and nothing loads. When I removed the sound from the application, it works when exported etc.

I got the code below online, and modded it a bit to suit my needs. It seemed nice and easy but for some reason it won't work. As I said, in my ide(Eclipse) when I run it, ecerything works fine...

I've used WinRAR and 'jar tf' to check if the file is being bundled with the .jar and it is.

When I run from the command line I get a NullPointerException

java.lang.NullPointerException
        at mr.myapp.Sound.loop(Sound.jav
        at mr.myapp.GameComponent.run(Ga
        at java.lang.Thread.run(Unknown Source)

Here is the code

package mr.myapp.Sound;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; 



public class Sound {

    public static Sound main_menu = loadSound("/test.wav");
    public static Sound game = loadSound("/song12.wav");
    public static Sound shoot = loadSound("/shoot.wav");
    public static Sound hit = loadSound("/hit.wav");
    public static Sound fail = loadSound("/fail.WAV");

    private Clip clip;

    public static Sound loadSound(String fileName) {
        Sound sound = new Sound();
        try {
            //AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));
            AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(fileName));
            Clip clip = AudioSystem.getClip();
            clip.open(ais);
            sound.clip = clip;
        } catch (Exception e) {
            System.out.println(e);
        }
        return sound;
    }

    public void play(){
        if(clip == null) return;
        stop();
        clip.setFramePosition(0);
        clip.start();
    }

    public void stop(){
        if(clip.isRunning()) clip.stop();
    }

    public void close(){
        stop();
        clip.close();
    }

    public void loop(){
        clip.loop(Clip.LOOP_CONTINUOUSLY);
     }

    public boolean isActive(){
        return clip.isActive();
     }
}
Thorin Schiffer
  • 2,818
  • 4
  • 25
  • 34
mike73
  • 53
  • 1
  • 6

2 Answers2

1

I'm gonna guess that it's attempting to load the files from the local temp directory that the .jar file is loaded into, rather than from within the archive itself.

What you want to do is load the resource from within the archive, which you can do using the java.lang.Class.getResourceAsStream(String) function. Here's another question that discusses that kind of thing.

Community
  • 1
  • 1
Polynomial
  • 27,674
  • 12
  • 80
  • 107
  • Oh wow. I copied the sound file to the same location as t he jar and it worked. In my original code, i used getResourceAsStream(fileName) instead of getResource(fileName) but it doesn't change anything – mike73 Mar 09 '14 at 19:21
0

I figured it out. my sound file was .WAV and not .wav as i put in the sound class.

Also i had to use getResource and not getResourceAsStream

mike73
  • 53
  • 1
  • 6