This is just the test file, I have my Sound class and I am invoking it from another main class. It plays for 3 sec, and then it stops, then it doesn't play again. What might be the issue?
This is driving me crazy.
Sound Class
public class Sound {
private static URL url1, url2, url3, url4;
private static AudioClip gun, boom;
private static Clip menuClip, gameClip;
private static AudioInputStream menu, game;
public static boolean menuPlaying, gamePlaying, explosionOn;
public Sound() {
menuPlaying = false;
gamePlaying = false;
url1 = Sound.class.getResource("/laserSound.wav");
url2 = Sound.class.getResource("/boom.wav");
url3 = Sound.class.getResource("/adventure.wav");
url4 = Sound.class.getResource("/adventure.wav");
gun = Applet.newAudioClip(url1);
boom = Applet.newAudioClip(url2);
try {
game = AudioSystem.getAudioInputStream(url4);
//menu = AudioSystem.getAudioInputStream(url3);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void playGunSound() {
gun.play();
}
public void stopGunSound() {
gun.stop();
}
public void playExplosion() {
boom.stop();
boom.play();
}
public void stopExplosion() {
boom.stop();
}
public void playGameMusic() throws LineUnavailableException {
if (gamePlaying == false) {
gameClip = AudioSystem.getClip();
try {
gameClip.open(game);
} catch (
LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
gameClip.start();
gameClip.loop(Clip.LOOP_CONTINUOUSLY);
gamePlaying = true;
}
}
public void stopGameMusic() {
gameClip.stop();
gamePlaying = false;
}
}
Main Method
public static void main(String[] args) throws LineUnavailableException, InterruptedException {
Sound sound = new Sound();
sound.playGameMusic();
TimeUnit.SECONDS.sleep(3);
sound.stopGameMusic();
TimeUnit.SECONDS.sleep(3);
sound.playGameMusic();
}