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 after some delay about of 20 - 30 seconds. Here is my code to play video
-(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 anybody know its answer. this code is working perfectly in iOS 9 but not iOS 10. Thanks in advance.
Try to set automaticallyWaitsToMinimizeStalling property of AVPlayer to NO in order to start playback immediately.
_avPlayer = [[AVPlayer alloc]init]; //WithPlayerItem:_avPlayerItem];
_avPlayer.automaticallyWaitsToMinimizeStalling = NO;
But if sufficient content is not available for playing then player might stall.
Apple documentation: https://developer.apple.com/reference/avfoundation/avplayer/1643482-automaticallywaitstominimizestal.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With