Name: Geronimo 2011-09-30 15:47
Hello, i was wondering if anyone could help me. I'm making a java game and i need a sound to start when i press a key and stop when i release it. What happens is that once the music starts it wont stop when i release :l
heres the code:
public void music() {
try {
InputStream in = new FileInputStream("C:/users/Usuario/Pictures/Game/nyan.wav");
AudioStream as = new AudioStream(in);
if (shooting == true && playingMusic == false) {
AudioPlayer.player.start(as);
playingMusic = true;
}
if (playingMusic == true && shooting == false) {
AudioPlayer.player. stop(as);
playingMusic = false;
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
the boolean playingMusic turns false when i release so the codes working, but the sound wont stop
heres the code:
public void music() {
try {
InputStream in = new FileInputStream("C:/users/Usuario/Pictures/Game/nyan.wav");
AudioStream as = new AudioStream(in);
if (shooting == true && playingMusic == false) {
AudioPlayer.player.start(as);
playingMusic = true;
}
if (playingMusic == true && shooting == false) {
AudioPlayer.player. stop(as);
playingMusic = false;
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
the boolean playingMusic turns false when i release so the codes working, but the sound wont stop