8

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.

pedrouan
  • 12,762
  • 3
  • 58
  • 74
Jagveer Singh
  • 2,258
  • 19
  • 34

4 Answers4

6

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.

  • sorry:( Did you see this question? May be it will help you. http://stackoverflow.com/questions/31225580/long-delay-before-seeing-video-when-avplayer-created-in-exportasynchronouslywith – Eugene Butkevich Sep 29 '16 at 07:02
1

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

Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74
1

Im facing the same problem and my solution is take old code into 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.someone thinks that AVVideoCompositionCoreAnimationTool is the cause of the issuehttps://forums.developer.apple.com/thread/62521. I'm not sure,but i do use it.

Just try it!

Hope this helps!

pfcstyle
  • 97
  • 5
1

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

nice_pink
  • 435
  • 1
  • 3
  • 16