JavaFX Media - pause();方法使MediaPlayer快进? [英] JavaFX Media - pause(); method makes MediaPlayer fast-forward?

查看:474
本文介绍了JavaFX Media - pause();方法使MediaPlayer快进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

暂停() MediaPlayer 的方法使媒体寻求一点。
这真的很烦人,但我没有找到问题所在。

the pause() method of MediaPlayer makes the Media "seek" a bit. It's really annoying but I didn't find out where the problem is.

    private void playPauseClicked() 
    {     
        Status currentStatus = player.getStatus();
        if(currentStatus == Status.PLAYING)
        {
            Duration d1 = player.getCurrentTime(); //To measure the difference
            player.pause();
            Duration d2 = player.getCurrentTime();
            VIDEO_PAUSED = true;
        }
        else if(currentStatus == Status.PAUSED || currentStatus == Status.STOPPED)
        {
            player.play();
            VIDEO_PAUSED = false;
        }
    }

结果不明确,大概是200-点d1和d2之间有400ms的差异。

The result is not clear, it's something about 200-400ms difference between spot d1 and d2.

当然我试图在暂停媒体后将我的播放器恢复到d1,没有工作,恢复媒体后的结果相同。

Of course I tried to seek my player back to d1 after pausing the media, didn't work, same result after resuming the media.

提前感谢任何建议:)

推荐答案

这是因为暂停方法不会立即停止播放(当调用 pause 时,媒体仍会播放并在 statusProperty 更改):

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):


暂停播放器。 一旦播放器实际暂停,状态将
设置为MediaPlayer.Status.PAUSED。

因此测量并不是真正相关的,因为当你在暂停之前得到当前时间持续时间d1 = player.getCurrentTime(); 时,播放实际上并没有暂停,当你在暂停之后得到当前时间持续时间d2 = player.getCurrentTime(); 不是正确,因为暂停已异步执行,因此没有任何保证在 d2 视频停止播放:

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:


MediaPlayer的操作本质上是异步的。

The operation of a MediaPlayer is inherently asynchronous.

如果在播放停止时获得正确的时间,你可以做的是观察 MediaPlayer 状态的变化,例如使用例如 setOnPaused

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.

示例:

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

示例输出:

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

注意:但是不需要设置<$的时间c $ c> MediaPlayer 如果您只想停下来玩,但如果您愿意,可以使用寻求方法。

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.

这篇关于JavaFX Media - pause();方法使MediaPlayer快进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆