Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 834
Jagveer Singh Avatar asked Sep 29 '16 05:09

Jagveer Singh


1 Answers

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.

like image 110
Eugene Butkevich Avatar answered Sep 20 '22 14:09

Eugene Butkevich