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();
}
}