I'm very new and don't know how to stop my Music in another class. My idea was that I push one button and the music should stop.
public class Musik {
public static synchronized void music(String song) {
final String songname= song;
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try{
Clip clip = AudioSystem.getClip();
AudioInputStream input=AudioSystem.getAudioInputStream(new File(songname));
clip.open(input);
clip.loop(clip.LOOP_CONTINUOUSLY);
Thread.sleep(clip.getMicrosecondLength()/1000);
}catch (Exception e){
e.printStackTrace();
}
}
}
}).start();
}
}
I want in the button that the music stops, but I don't have any clue. stop();
, close();
or something like that doesn't function at all.
public void actionPerformed(ActionEvent e){
if (button==e.getSource()){
//Musik.close(); doesnt work
}