JavaFX Media - pause(); method makes MediaPlayer fast-forward?

99封情书 提交于 2019-12-06 16:42:24

It's because the pause method does not stop the playing instantly (when pause is called, the media still plays and stops exactly at the moment when the statusProperty changes):

Pauses the player. Once the player is actually paused the status will be set to MediaPlayer.Status.PAUSED.

Therefore the measurements are not really relevant, because when you get the current time Duration d1 = player.getCurrentTime(); before the pause, the playing is not actually paused and also when you get the current time after the call of pause with Duration d2 = player.getCurrentTime(); is not correct, as pause has been executed asynchronously therefore there is no garantie that at d2 the video stopped to play:

The operation of a MediaPlayer is inherently asynchronous.

What you could do to get the correct time when the playing is stopped is to observe the changes of the status of the MediaPlayer using e.g. setOnPaused.

Example:

private void playPauseClicked() {
    Status currentStatus = player.getStatus();

    if(currentStatus == Status.PLAYING)
        player.pause();
    else if(currentStatus == Status.PAUSED || currentStatus == Status.STOPPED) {
        System.out.println("Player will start at: " + player.getCurrentTime());
        player.play();
    }
}

player.setOnPaused(() -> System.out.println("Paused at: " + player.getCurrentTime()));

Sample output:

Paused at: 1071.0203000000001 ms
Player will start at: 1071.0203000000001 ms
Paused at: 2716.7345 ms
Player will start at: 2716.7345 ms

Note: However it is not needed to set the time of the MediaPlayer in the case if you just want to stop and play, but if you want to you can use seek method.

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