我可以在IOS4 AVPlayer seekToTime完成时进行测试吗?

use*_*279 3 video avfoundation ios4 avplayer

我正在AVFoundation用来实现一个AVPlayer.我想连续循环播放视频片段,所以我注册一个AVPlayerItemDidPlayToEndTimeNotification来调用这个方法:

- (void)player1ItemDidReachEnd:(NSNotification *)notification
{ 
 dispatch_async(dispatch_get_main_queue(),
       ^{
        [player1 seekToTime:kCMTimeZero]; 
        [player1 play];
       });
}
Run Code Online (Sandbox Code Playgroud)

它在某些时候有效,但最终会停止播放,大概是因为异步完成了seekToTime.如何使此代码防弹?

use*_*279 5

我现在通过将AVPlayer的AVPlayerActionAtItemEnd属性设置为AVPlayerActionAtItemEndNone来修复此问题 - 默认值为AVPlayerActionAtItemEndPause.AVPlayer不再在剪辑结束时暂停,因此无需重新启动播放.还发现没有必要将seekToTime分派到主队列.


sin*_*h99 5

player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]];

(void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
 [p seekToTime:kCMTimeZero];
}   
Run Code Online (Sandbox Code Playgroud)