Further:
First
I add an observer
- (void)attachWatcherBlock { [self removeWatcherBlock]; if (self.videoPlayer) { __weak typeof(self) wSelf = self; self.timeObserver = [self.videoPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, NSEC_PER_SEC) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) { if (wSelf.playerBlock && wSelf.videoPlayer) { CGFloat playTime = CMTimeGetSeconds(wSelf.videoPlayer.currentTime); CGFloat duration = CMTimeGetSeconds(wSelf.videoPlayer.currentItem.duration); if (playTime > 0.0f) { [wSelf replaceCoverToVideo]; } wSelf.playerBlock(wSelf, playTime, duration); } }]; } [self.videoPlayer play]; }
And then , if the playTime block is equal to the duration of the callback
- (void)replay { __weak typeof(self) wSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ __strong typeof(wSelf) self = wSelf; if (self.videoPlayer) { [self.videoPlayer seekToTime:kCMTimeZero]; } }); }
All this in my subclass of UIView is called NDVideoPlayerView
source share