I'm writing a videogame and I want to stop the music when a player loses or quits (to the main menu).
Here's my code:
public class Music{
    private static Clip clip;
    private static AudioInputStream stream;
    private static void loadMusic(){
        if(clip != null) return;
        try {
            AudioFormat format;
            DataLine.Info info;
            stream = AudioSystem.getAudioInputStream(Music.class.getResource("/resources/music/music.wav"));
            format = stream.getFormat();
            info = new DataLine.Info(Clip.class, format);
            clip = (Clip) AudioSystem.getLine(info);
            clip.open(stream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void playMusic(boolean loop){
        loadMusic();
        if(clip == null) return;
        if(loop) clip.loop(Clip.LOOP_CONTINUOUSLY);
        else clip.start();
    }
    public static void stopMusic(){
        clip.stop();
        clip.setMicrosecondPosition(0);
    }
}
Whenever I call Music.stopMusic(), the game hangs for a few seconds then continues.
 
    