我有一个完美的音乐播放器,但我想添加一个播放/暂停按钮。我已经设置了按钮,但我不知道实际暂停剪辑的代码。
这是我的代码:
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!");}
提前致谢!
答案 0 :(得分:2)
您需要在clip.start();
之后致电clip.open(ais)
,然后clip.stop()
才会有效。
答案 1 :(得分:1)
在停止剪辑之前,指定一个长变量来获取剪辑当前时间的值。
例如:
long clipTime;
clipTime= clip.getMicrosecondPostion();
clip.stop();
//When you want to resume the clip from the last position
clip.setMicrosecondPosition(clipTime);
clip.start();