I make a video games on eclipse and i will make a menu song and stop the song but clip.stop(); not work i don't know why please help me. The menu song Clip.play();and Clip.loop(Clip.LOOP_CONTINUOUSLY); work but i can stop the song only if i close my game.
package Audio;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import sun.audio.AudioPlayer;
public class MenuAudio {
public static boolean stopped = false;
public static void Start(){
System.out.println(stopped);
try{
File f = new File("C:\\Games\\Test\\Assets\\sound\\menu\\menuLoop.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
clip.open(ais);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
AudioPlayer.player.stop(ais);
if(stopped == true) // work
{
clip.stop(); //not work
clip.close(); //not work
stopped = false; //work
System.out.println(stopped);//work
}
}catch(Exception exception){System.out.println("WAV FILES NOT FOUND!! ");}
}
}
boolean stopped return to true in another class whit this code
int test = 0;
//code useless
........
//code useless
MenuAudio.Start(); // play the sound
//code useless
........
//code useless
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){ //work
test = 0;
}
if(test == 0 && Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){ //work
MenuAudio.stopped = true;
MenuAudio.Start();
test = 1;
}
this code is work the console return that : true false if i press the key Left after Right. I don't know why Clip.stop(); not work.
(Sorry for the bad english)