MPMoviePlayer setCurrentPlaybackTime iOS

我使用MPMoviePlayer播放我的video,我遇到的问题是当用户按下设备的主页按钮时,我保存了video的当前位置,而不是当他再次午餐时,我希望他从他离开的地方开始,所以我使用setCurrentPlaybackTime函数,它在iOS 5中完美运行,但在iOS 5.1中,video从头开始。

有没有人能解决这类问题?

我的问题解决了,我将解释到目前为止我所做的事情。

当我找不到该行为的解决方案/解释时(我发现它很奇怪)我开始进行大量的日志记录,所以我注意到当我记录当前位置时我总是(在iOS 5.1中)

currentPlaybackTime = nan 

但是在iOS 5中,我的正常值为0.00000

所以我做了一个计时器(重复自己)每次记录当前时间 ,所以我注意到它在一段时间后从nan变为0.00000

我得出的结论是,在设置PlaybackTime之前我需要等一下(不明白为什么),所以在等待1 ms (1/1000 s )后它就可以了。

您是否尝试使用seekToTime(仅限AvPlayer)而不是setCurrentPlaybackTime?

如下所示:
为什么MPMoviePlayerController setCurrentPlaybackTime进入错误的时间?

注意:此解决方案适用于AvPlayer而非MPMoviePlayer(即使很难在列出的链接中指出)。 但在切换到AvPlayer之前,我会等待其他答案。

我们应该在获取MPMoviePlayerLoadStateDidChangeNotification通知后设置currentPlaybackTime。

要知道是否加载了moviePLayer,首先需要注册MPMoviePlayerLoadStateDidChangeNotification

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; 

一旦电影播放器​​加载了movieLoadStateDidChange:将被调用,然后检查MPMovieLoadStatePlaythroughOK加载状态并设置currentPlaybackTime。

 - (void)movieLoadStateDidChange:(NSNotification *)notification { MPMoviePlayerController *player = notification.object; MPMovieLoadState loadState = player.loadState; /* Enough data has been buffered for playback to continue uninterrupted. */ if (loadState & MPMovieLoadStatePlaythroughOK) { //should be called only once , so using self.videoLoaded - only for first time movie loaded , if required. This function wil be called multiple times after stalled if(!self.videoLoaded) { self.moviePlayerController.currentPlaybackTime = self.currentPlaybackTime; self.videoLoaded = YES; } } }