How do i pause a clip? Java

安稳与你 提交于 2020-01-05 08:14:14

问题


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!


回答1:


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




回答2:


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();


来源:https://stackoverflow.com/questions/16915241/how-do-i-pause-a-clip-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!