当前播放时间的异常行为

4

我使用MPMoviePlayerController播放视频。

下面是视频缩略图列表。当我按下一个缩略图时,我希望使用setCurrentPlaybackTime跳转到视频的特定位置。

同时,我还有一个计时器,根据视频播放的位置更新所选缩略图,使用currentPlaybackTime实现。

问题在于:当调用setCurrentPlaybackTime时,播放器会先显示要前往的秒数,然后才跳转到指定的时间点。这需要几秒钟时间才能反映出新的时间。在此期间,用户体验非常差:按下一个缩略图后,它会被选中一段时间,然后计时器会更新到上一个缩略图,最后再跳回我选择的缩略图。

我尝试在计时器中使用以下代码:

if (moviePlayer.playbackState != MPMoviePlaybackStatePlaying && !(moviePlayer.loadState & MPMovieLoadStatePlaythroughOK)) return;

为了防止计时器在播放器处于从显示之前的缩略图到新缩略图的过渡阶段时更新所选的缩略图,但似乎并没有起作用。 "playbackState" 和 "loadState" 似乎完全不一致和不可预测。
1个回答

2
为解决这个问题,以下是我在其中一个项目中实现了这个令人讨厌的状态覆盖率的方法。这很讨厌和脆弱,但对我来说足够好用。
我使用了两个标志和两个时间间隔;
BOOL seekInProgress_; 
BOOL seekRecoveryInProgress_;
NSTimeInterval seekingTowards_;
NSTimeInterval seekingRecoverySince_;

以上所有内容都应默认为NO0.0
在开始搜索时:
//are we supposed to seek?
if (movieController_.currentPlaybackTime != seekToTime)
{   //yes->
    movieController_.currentPlaybackTime = seekToTime;
    seekingTowards_ = seekToTime;
    seekInProgress_ = YES;
}

在计时器回调函数中:

//are we currently seeking?
if (seekInProgress_)
{   //yes->did the playback-time change since the seeking has been triggered?
    if (seekingTowards_ != movieController_.currentPlaybackTime)
    {   //yes->we are now in seek-recovery state
        seekingRecoverySince_ = movieController_.currentPlaybackTime;
        seekRecoveryInProgress_ = YES;
        seekInProgress_ = NO;
        seekingTowards_ = 0.0;
    }
}
//are we currently recovering from seeking?
else if (seekRecoveryInProgress_)   
{   //yes->did the playback-time change since the seeking-recovery has been triggered?
    if (seekingRecoverySince_ != movieController_.currentPlaybackTime)
    {   //yes->seek recovery done!
        seekRecoveryInProgress_ = NO;
        seekingRecoverySince_ = 0.0;
    }
}

最终,MPMoviePlayerController 并不适合进行如此微观的管理。我不得不加入至少半打状态覆盖标志以应对各种情况,但我不建议在其他项目中重复这样做。一旦达到这个水平,考虑使用 AVPlayer 可能是一个好主意。


谢谢你的回答。我做了类似的事情,如果连接良好,也应该能够正常工作:在选择新的缩略图后停止计时器更新3秒钟。当然,这是在最短的视频可以为3秒钟的情况下。 - bashan
1
苹果的视频播放器表现得如此奇怪真是奇怪。至少我期望从播放器得到一个明确的状态或指示,显示正在寻找您想要的位置。 - bashan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接