我有一个可以每秒更新滑块位置的runnable,但是滑块在它应该控制的媒体之前到达终点。如何设置它以便在同一时间结束?
executorService.scheduleAtFixedRate(new UpdateRunnable(mediaPlayer), 0L, 1L, TimeUnit.SECONDS);
private final class UpdateRunnable implements Runnable {
private final MediaPlayer mediaPlayer;
private UpdateRunnable(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
@Override
public void run() {
if(!mediaPlayer.isPlaying())
{
System.out.println("Not playing");
return;
}
final long time = this.mediaPlayer.getTime();
final int position = (int)(this.mediaPlayer.getPosition() * 1000.0f);
mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void positionChanged(MediaPlayer mediaPlayer, float newPosition) {
currentPosition = newPosition;
}
});
time_counter++;
System.out.println("New Position is " + currentPosition);
System.out.println("New time is " + time_counter);
// Updates to user interface components must be executed on the Event
// Dispatch Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if(mediaPlayer.isPlaying()) {
//System.out.println("time = " + time);
updateTime(time);
updatePosition(position);
}
}
});
}
}
private void updatePosition(int value) {
timeSlider.setValue(value);
}
无论如何,我如何根据正在播放的媒体长度设置滑块的长度?