user2413200
user2413200

Reputation: 245

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!

Upvotes: 2

Views: 9975

Answers (2)

Omar
Omar

Reputation: 51

Before stopping the clip, assign a long variable to take the value of the current time of the clip.

For example:

long clipTime;

clipTime= clip.getMicrosecondPostion();

clip.stop();

//When you want to resume the clip from the last position

clip.setMicrosecondPosition(clipTime);

clip.start();

Upvotes: 5

David
David

Reputation: 20063

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

Upvotes: 4

Related Questions