为什么我不能依靠使用AVFoundation循环视频的NSNotificationCenter?

时间:2012-09-02 18:01:38

标签: objective-c video avfoundation nsnotificationcenter

在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错过了通知消息? 愚蠢的问题。但是我无法做出头脑或尾巴...

1 个答案:

答案 0 :(得分:0)

答案是

self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;  // default: AVPlayerActionAtItemEndPause