How to say when AVPlayer fully uploaded a video

I need to know when AVPlayer fully completed loading a video object so that there is no delay or freezing of the video. When I start playing the video, it plays halfway and then freezes. If I go out again and open it, it plays the rest of the video and will play the video smoothly. First I load the asset:

 AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; NSString *tracksKey = @"tracks"; [asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler: ^{ // Completion handler block. dispatch_async(dispatch_get_main_queue(), ^{ NSError *error; AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error]; if (status == AVKeyValueStatusLoaded) { AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset]; // Set Key-Observers } }); }]; 

I set three observer elements on an element and installed the player using the element

  [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:&ItemStatusContext]; [item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:&playbackLikelyToKeepUp]; [item addObserver:self forKeyPath:@"playbackBufferFull" options:NSKeyValueObservingOptionNew context:&playbackBufferFull]; self.player = [AVPlayer playerWithPlayerItem:item]; 

Next, I check if the player / element is ready for the game, and also if playBackLiciousToKeepUp is true.

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ((context == &ItemStatusContext) || (context == &playbackLikelyToKeepUp)) { dispatch_async(dispatch_get_main_queue(), ^{ if ((self.player.currentItem != nil) && ([self.player.currentItem status] == AVPlayerItemStatusReadyToPlay) && (self.player.currentItem.playbackLikelyToKeepUp == YES) && (self.player.status == AVPlayerStatusReadyToPlay)) { NSLog(@"CALLED TO PLAY"); [self prerollVideoPlayer]; } else { [self.spinner startAnimating]; } }); return; } else if (context == &playbackBufferFull){ if ((self.player.currentItem.playbackBufferFull) && (self.player.status == AVPlayerStatusReadyToPlay)) { NSLog(@"Full Play"); [self prerollVideoPlayer]; } return; } [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; return; } 

Finally, I call preroll and start playing when I'm done.

 -(void)prerollVideoPlayer{ [self.player prerollAtRate:1.0 completionHandler:^(BOOL finished){ if (finished) { [self.spinner stopAnimating]; [self.player play]; } }]; } 

But I still get the video delay, and AVPlayer freezes halfway. I’m trying to find out when the player is fully loaded or at least enough so that the video can be started without a pause halfway. Thanks for the help.

+5
source share

Source: https://habr.com/ru/post/1235403/


All Articles