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.如何使此代码防弹?
我现在通过将AVPlayer的AVPlayerActionAtItemEnd属性设置为AVPlayerActionAtItemEndNone来修复此问题 - 默认值为AVPlayerActionAtItemEndPause.AVPlayer不再在剪辑结束时暂停,因此无需重新启动播放.还发现没有必要将seekToTime分派到主队列.
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)
| 归档时间: |
|
| 查看次数: |
4576 次 |
| 最近记录: |