Why can't I rely on NSNotificationCenter using AVFoundation to loop video?

余生颓废 提交于 2019-12-11 23:19:31

问题


I loop my video played with AVFoundation video with the help of NSNotificationCenter and playerItemDidReachEnd:

    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];

and

- (void)playerItemDidReachEnd:(NSNotification *)notification {        
        [notification.object seekToTime:kCMTimeZero];
        [self startVideoLoop];
}

works great - but unfortunately not reliable. some times without any logic reason (seems to me) the player stops at the end of the movie without restarting again.

is it possible that NSNotificationCenter misses the end of the movie or the method playerItemDidReachEnd misses the notification message? stupid question. but I am unable to make head nor tail of it...


回答1:


the answer ist

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


来源:https://stackoverflow.com/questions/12238429/why-cant-i-rely-on-nsnotificationcenter-using-avfoundation-to-loop-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!