I'm trying to make music play with one button(First click is play, second is stop, third is play again etc etc). The problem is it does not want to stop. Second click is blank and then third music starts to impose itself. Any suggestions?
public void music() {
File file = new File("music.wav");
AudioInputStream audioStream = null;
Clip clip = null;
try {
audioStream = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioStream);
} catch (LineUnavailableException e) {
throw new RuntimeException(e);
} catch (UnsupportedAudioFileException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (m % 2 == 1) {
clip.start();
m++;
} else {
clip.stop();
m++;
}
}