Search code examples
javaaudiojavasound

How do i pause a clip? Java


I have a music player that works perfectly fine but i want to add a play/pause button. I have set the button up and all that but i don't know the code to actually pause the clip.

Here is my code:

        try{
        File f = new File("songs/mysong.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.getAudioInputStream(f);
        clip.open(ais);
        playing = true;
        if(MusicPlayer.pause)
        {
            clip.stop(); // <- Doesnt stop the song
        } 
        clip.loop(Clip.LOOP_CONTINUOUSLY);

    }catch(Exception exception){System.out.println("Failed To Play The WAV File!");}

Thanks in advance!


Solution

  • You need to call clip.start(); after clip.open(ais) then clip.stop() will work.