AVPlayer does not play video Instantly on iOS 10, while audio playing only

后端 未结 4 932
南方客
南方客 2021-02-09 00:15

I am creating video with AVAssetExportSession and playing video when it finishes. But Visual Part not showing instantly but only audio is playing instantly. Visual part comes af

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-09 00:39

    I do next:

    First

    I add watcher

    - (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 in block playTime equals duration call replay

    - (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 UIView subclass called NDVideoPlayerView

提交回复
热议问题