JavaFX 媒体 - 暂停();方法使 MediaPlayer 快进?
JavaFX Media - pause(); method makes MediaPlayer fast-forward?
MediaPlayer
的 pause()
方法使 Media
"seek" 有点。
真烦人,但我没有找到问题所在。
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;
}
}
结果不清楚,点d1和点d2之间大约有200-400ms的差异。
当然,我在暂停媒体后尝试将我的播放器搜索回 d1,但没有成功,恢复媒体后结果相同。
提前感谢您的任何建议:)
因为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发生了变化):
Pauses the player. Once the player is actually paused the status will
be set to MediaPlayer.Status.PAUSED.
因此测量并不是真正相关的,因为当您在暂停前获得当前时间 Duration d1 = player.getCurrentTime();
时,播放实际上并没有暂停,而且当您在调用 [=12] 之后获得当前时间时=] 和 Duration d2 = player.getCurrentTime();
不正确,因为 pause
是异步执行的,因此无法保证视频在 d2
停止播放:
The operation of a MediaPlayer is inherently asynchronous.
要在播放停止时获得正确的时间,您可以做的是观察 MediaPlayer
状态的变化,例如使用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
注意:不过如果只是想停下来玩玩的话MediaPlayer
的时间是不需要设置的,但是如果你想你可以使用 seek 方法。
MediaPlayer
的 pause()
方法使 Media
"seek" 有点。
真烦人,但我没有找到问题所在。
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;
}
}
结果不清楚,点d1和点d2之间大约有200-400ms的差异。
当然,我在暂停媒体后尝试将我的播放器搜索回 d1,但没有成功,恢复媒体后结果相同。
提前感谢您的任何建议:)
因为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发生了变化):
Pauses the player. Once the player is actually paused the status will be set to MediaPlayer.Status.PAUSED.
因此测量并不是真正相关的,因为当您在暂停前获得当前时间 Duration d1 = player.getCurrentTime();
时,播放实际上并没有暂停,而且当您在调用 [=12] 之后获得当前时间时=] 和 Duration d2 = player.getCurrentTime();
不正确,因为 pause
是异步执行的,因此无法保证视频在 d2
停止播放:
The operation of a MediaPlayer is inherently asynchronous.
要在播放停止时获得正确的时间,您可以做的是观察 MediaPlayer
状态的变化,例如使用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
注意:不过如果只是想停下来玩玩的话MediaPlayer
的时间是不需要设置的,但是如果你想你可以使用 seek 方法。