在 NSNotificationCenter 和 playerItemDidReachEnd 的帮助下,我循环播放了使用 AVFoundation 视频播放的视频:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
[self.avPlayer addObserver:self forKeyPath:@"status"
options:0 context:AVMoviePlayerViewControllerStatusObservationContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
[self.VideoView setPlayer:self.avPlayer];
和
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[notification.object seekToTime:kCMTimeZero];
[self startVideoLoop];
}
效果很好 - 但不幸的是不可靠。有时没有任何逻辑原因(在我看来)播放器在电影结束时停止而没有再次重新启动。
NSNotificationCenter 是否有可能错过了电影的结尾或方法 playerItemDidReachEnd 错过了通知消息?愚蠢的问题。但我无法确定它的头或尾......