如何判断AVPlayer何时完全加载了视频

时间:2015-11-07 00:17:08

标签: ios avplayer nsurl avasset

我需要知道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 } }); }]; 何时完全加载了视频资源,因此没有视频延迟/冻结。当我开始播放视频时,它播放一半然后冻结。如果我退出并再次打开,它将播放视频的其余部分,并将平滑地重复播放视频。首先我加载资产:

 [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];

我在项目上设置了三个关键观察者,并使用项目

设置了玩家
- (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;
}

接下来,我检查播放器/项目是否准备好播放,以及playBackLikelyToKeepUp是否为真。

preroll

最后我打电话给-(void)prerollVideoPlayer{ [self.player prerollAtRate:1.0 completionHandler:^(BOOL finished){ if (finished) { [self.spinner stopAnimating]; [self.player play]; } }]; } 并在完成后开始播放。

AVPlayer

但是我仍然会遇到视频延迟而www.example.com/posts/1 www.example.com/about 冻结了一半。我试图找出播放器何时完全装满,或者至少足以让视频在没有中途停顿的情况下开始播放。谢谢你的帮助。

0 个答案:

没有答案