预加载视频即可播放 [英] Preloading video to play without delay

查看:169
本文介绍了预加载视频即可播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于视频预加载的主题,但对我来说仍然不是很清楚。



目标:


  1. 从网络加载视频,提供URL

  2. 等待完全加载视频

  3. 立即播放视频(因为我说它已经100%缓存)

理想情况下,计算下载速度,预测fe当缓冲60%的视频时,我们开始播放,40%将在没有延迟的情况下进行缓冲。



我尝试的内容:

  NSURL * url = [NSURL URLWithString:@video url address here]; 
AVURLAsset * avasset = [[AVURLAsset alloc] initWithURL:url options:nil];

AVPlayerItem * item = [[AVPlayerItem alloc] initWithAsset:avasset];
self.player = [[AVPlayer alloc] initWithPlayerItem:item];

self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
CGSize size = self.view.bounds.size;
float x = size.width / 2.0-202.0;
float y = size.height / 2.0 - 100;

self.playerLayer.frame = CGRectMake(x,y,404,200);
self.playerLayer.backgroundColor = [UIColor blackColor] .CGColor;

[self.view.layer addSublayer:self.playerLayer];
NSString * tracksKey = @tracks;

[avasset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
^ {
dispatch_async(dispatch_get_main_queue(),
^ {
NSError * error = nil;
AVKeyValueStatus status = [avasset statusOfValueForKey:tracksKey error:& error];
NSLog(@Status%d,status);
if(status == AVKeyValueStatusLoaded){
[self.player play];
}
else {
NSLog(@未加载资产的曲目:\ n%@,[error localizedDescription]);
}
});
}];
}

视频开始播放和慢速连接挂起,但如果我们开始播放,让我们说1分钟之后,它会很好地发挥。



主要问题 - 是否能够在没有延迟的情况下开始播放直到结束时获得通知?



注意:不管它是 AVFoundation还是MPMovieController



<并不重要p>我假设只能通过单独下载视频,在本地存储然后播放来完成。负面因素 - 我们无法在下载整个文件之前开始播放。

解决方案

你可以在AVPlayerItem上进行键值观察playbackLikelyToKeepUp,playbackBufferEmpty和playbackBufferFull属性可以了解玩家的状态。在 docs <中查看有关这些值何时为真的详细信息/ A>。值得注意的是:


当属性playbackBufferFull指示YES时,playbackLikelyToKeepUp可以指示NO。在这种情况下,回放缓冲区已达到容量,但没有统计数据支持预测回放可能在将来保持。您可以决定是否继续播放媒体。



there are tons of topics on SO about video preloading but still isn't crystal clear for me.

Objectives:

  1. Load video from the network, URL is given
  2. Wait for video loaded completely
  3. Play video without delay (as I said it's already buffered 100%)

Ideally, calculate download speed, predict f.e when buffered 60% of video, we start playing and 40% will be buffered while playing without delay.

what I tried:

 NSURL *url = [NSURL URLWithString:@"video url address here"];
 AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:url options:nil];

 AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:avasset];
 self.player = [[AVPlayer alloc] initWithPlayerItem:item];

 self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
 CGSize size = self.view.bounds.size;
 float x = size.width/2.0-202.0;
 float y = size.height/2.0 - 100;

 self.playerLayer.frame = CGRectMake(x, y, 404, 200);
 self.playerLayer.backgroundColor = [UIColor blackColor].CGColor;

 [self.view.layer addSublayer:self.playerLayer];
 NSString *tracksKey = @"tracks";

 [avasset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
     ^{
         dispatch_async(dispatch_get_main_queue(),
                        ^{
                            NSError *error = nil;
                            AVKeyValueStatus status = [avasset statusOfValueForKey:tracksKey error:&error];
                                NSLog(@"Status %d", status);
                            if (status == AVKeyValueStatusLoaded) {
                                [self.player play];
                            }
                            else {
                                NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
                            }
                        });
     }];
}

Video starts playing and on slow connection hangs out, but if we start playing, let's say after 1 minute, it plays nicely.

The main question - is there ability to get notified when it can be start and play till the end without delaying?

Note: not really important whether it's a AVFoundation or MPMovieController

My assuming that it only can be done by downloading video separately, storing it locally and then playing. Negative aspect there - we can't start playing until whole file is downloaded.

解决方案

You could key-value observe on the AVPlayerItem's playbackLikelyToKeepUp, playbackBufferEmpty, and playbackBufferFull properties to get a sense of your player's status. See finer details about when these values are true in the docs. Notably:

It is possible for playbackLikelyToKeepUp to indicate NO while the property playbackBufferFull indicates YES. In this event the playback buffer has reached capacity but there isn't the statistical data to support a prediction that playback is likely to keep up in the future. It is up to you to decide whether to continue media playback.

这篇关于预加载视频即可播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆