AVPlayer does not play video Instantly on iOS 10, whereas only audio playback

I create a video with AVAssetExportSession and play the video when it ends. But the Visual Part is not displayed instantly, but only the sound is played instantly. The visual part appears after some delay of about 20-30 seconds. Here is my video playback code

-(void)playUrl:(NSURL *)vUrl{ [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[_avPlayer currentItem]]; _avAsset=nil; _avPlayerItem=nil; _avPlayer =nil; [_avPlayerLayer removeFromSuperlayer]; _avPlayerLayer=nil; _avAsset=[AVAsset assetWithURL:vUrl]; _avPlayerItem =[[AVPlayerItem alloc]initWithAsset:_avAsset]; _avPlayer = [[AVPlayer alloc]init]; //WithPlayerItem:_avPlayerItem]; [_avPlayer replaceCurrentItemWithPlayerItem:_avPlayerItem]; _avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:_avPlayer]; [_avPlayerLayer setFrame:CGRectMake(0, 0, viewAVPlayer.frame.size.width, viewAVPlayer.frame.size.height)]; [viewAVPlayer.layer addSublayer:_avPlayerLayer]; [_avPlayer seekToTime:kCMTimeZero]; [_avPlayer play]; _avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(repeatPlayer:) name:AVPlayerItemDidPlayToEndTimeNotification object:[_avPlayer currentItem]]; } 

please let me know if anyone knows his answer. this code works fine in iOS 9 but not iOS 10. Thanks in advance.

+6
source share
4 answers

Try setting autoWaitsToMinimizeStalling to AVPlayer property NO to start playback immediately.

 _avPlayer = [[AVPlayer alloc]init]; //WithPlayerItem:_avPlayerItem]; _avPlayer.automaticallyWaitsToMinimizeStalling = NO; 

But if enough content is not available for the game, the player can stop.

Apple Documentation: https://developer.apple.com/reference/avfoundation/avplayer/1643482-automaticallywaitstominimizestal .

Hope this helps.

+6
source

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

+1
source

Im facing the same problem, and my solution takes the old code into the main thread:

  -(void)ExporterManager:(DoCoExporterManager *)manager DidSuccessComplementWithOutputUrl:(NSURL *)outputUrl{ //... dispatch_async(dispatch_get_main_queue(), ^{ [_playView setContentUrl:outputUrl.path]; }); //... } 

Im using exportAsynchronouslyWithCompletionHandler to process my video. Some people believe that AVVideoCompositionCoreAnimationTool is the cause of the https://forums.developer.apple.com/thread/62521 problem. I'm not sure, but I use it.

Just give it a try!

Hope this helps!

+1
source

AVVideoCompositionCoreAnimationTool when used in AVAssetExportSession interferes with AVPlayer in iOS 10.0 - 10.1.1. iOS 10.2 fixes this error and your code should work fine.

+1
source

All Articles