Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 766
user2413200 Avatar asked Jun 03 '26 08:06

user2413200


1 Answers

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();
like image 165
Omar Avatar answered Jun 05 '26 02:06

Omar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!