gpt4 book ai didi

ios - 预加载视频以立即播放

转载 作者:行者123 更新时间:2023-12-01 15:59:55 28 4
gpt4 key购买 nike

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

目标:

  1. 从网络加载视频,给定URL
  2. 等待视频完全加载
  3. 立即播放视频(正如我所说,它已经缓冲了 100%)

理想情况下,计算下载速度,预测 f.e 当缓冲 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(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
}
});
}];
}

视频开始播放,连接速度慢时挂断,但如果我们开始播放,比方说 1 分钟后,播放效果很好。

主要问题 - 是否有能力在可以开始并播放到结束时得到通知而不会延迟?

注意:它是 AVFoundation 还是 MPMovieController 并不重要

我假设只能通过单独下载视频,将其存储在本地然后播放来完成。消极方面 - 在下载整个文件之前我们无法开始播放。

最佳答案

您可以对 AVPlayerItem 的 playbackLikelyToKeepUp、playbackBufferEmpty 和 playbackBufferFull 属性进行键值观察,以了解播放器的状态。在 docs 中查看有关这些值何时为真的更详细信息.特别是:

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.

关于ios - 预加载视频以立即播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26037132/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com