在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错过了通知消息? 愚蠢的问题。但是我无法做出头脑或尾巴...
答案 0 :(得分:0)
答案是
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; // default: AVPlayerActionAtItemEndPause