是否可以使用AVPlayer缓存HLS段? [英] Is it possible to cache HLS segments with AVPlayer?

查看:105
本文介绍了是否可以使用AVPlayer缓存HLS段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根本问题

在iOS中搜索时,我们的视频缓冲很多.与我们的网络播放器相比,它的缓冲空间要大得多,后者可以将已经观看过的片段的副本保存在临时存储中.

Our video buffers a lot when seeking in iOS. It buffers quite a bit more than our web player which saves copies of the already watched segments in temp storage.

所需的解决方案

在设备磁盘上本地缓存视频片段.我们可以缓存单个质量并始终重播它.

Caching the video segments locally on the device's disk. We're fine with caching a single quality and always replaying it.

阻止者

我们找不到在AVFoundation/AVPlayer中执行缓存的方法.

We can't find a way to perform caching within AVFoundation/AVPlayer.

我们尝试过的事情

使用AVPlayer拦截网络请求的2种方法.

2 ways to intercept networking requests with AVPlayer.

  1. 符合AVAssetResourceLoaderDelegate并手动处理媒体加载

不适用于HLS.您可以通过实现AVAssetResourceLoaderDelegate来加载m3u8文件,该文件允许您通过身份验证或解密响应,但是无法加载.ts文件. 这是我们尝试的代码: https://gist.github.com/nathanhillyer/84e46152d7c4c88183b6

Doesn't work with HLS. You can load the m3u8 files by implementing AVAssetResourceLoaderDelegate​, which allows you to pass authentication or to decrypt the response, however the .ts files can't be loaded. Here's the code we tried: https://gist.github.com/nathanhillyer/84e46152d7c4c88183b6

  1. 实施NSURLProtocol来捕获对.ts文件的请求.
  1. Implementing a NSURLProtocol ​to capture requests for .ts files.

AVURLAsset实际上避免被拦截.某种程度上,网络请求不会被捕获. (不知道为什么)

AVURLAsset actually avoids being intercepted. Somehow the network requests just don't get captured. (No clue why)

推荐答案

让我们从一个真正的好消息开始-iOS 10及更高版本-开箱即用. 很快就不再需要黑客了.在以下WWDC16会话中,可以找到有关HTTP Live Streaming的新功能的更多详细信息: https://developer.apple.com/videos/play/wwdc2016/504/

Let's start with really good news - iOS 10 and up - gives this out of the box. No more need for hacks soon. More details can be found in the following WWDC16 session about whats new in HTTP Live Streaming: https://developer.apple.com/videos/play/wwdc2016/504/

现在回到当前状态-iOS 9及更低版本: 使用AVPlayer可以.但是您可以通过本地HTTP服务器缓存HLS段,并使用AVPlayer播放本地流.

Now back to the current state of things - iOS 9 and lower: With AVPlayer, no. But you can cache HLS segments via a local HTTP server and play the local stream with AVPlayer.

在处理HLS播放时,AVPlayer和AVAsset不包含必要的信息(例如,其行为不同于MP4静态文件).

AVPlayer and AVAsset don't contain the necessary information when dealing with HLS playback (It behaves differently than a MP4 static file for example).

TL; DR-您需要使用HTTP请求来获取分段,并使用本地HTTP Server为它们提供服务.

TL;DR - You need to use HTTP requests to get the segments and serve them using a local HTTP Server.

包括我正在工作的公司在内的一些公司正在采用这种策略.

A few companies, including the one I'm working for, are using this strategy.

使用连接以所需的质量下载分段,重建清单并将其全部平整为一个目录和一种质量,然后使用应用程序内的本地http服务器将其提供给AVPlayer(AVPlayer只能播放HLS通过HTTP服务的流-而不是来自文件资产).

Use a connection to download the segments at the quality you want, rebuild the manifest and flatten it all into one directory and one quality and then use a local http server inside the app to serve it to AVPlayer (AVPlayer can only play HLS streams served over HTTP - not from file assets).

存在一些极端情况,例如,如果要一次播放和下载,请进行缓冲,正确重建m3u8清单,以及使用磁盘读取时出现不同的AVPlayer状态.

There are edge cases, such as, buffering if you want to play and download in one run, rebuilding the m3u8 manifest correctly, and different AVPlayer states with disk reading.

我从第一手的知识中发现了这一点,他们拥有这样的系统已经生产了5年,并且在App Store中的其他视频产品都使用相同的解决方案-可以为许多用户提供服务.

I've found this out from first hand knowledge, both having such a system in production for 5 years and other video products in the App Store that use the same solution - in total serving many users.

这也是我们为Android找到的最佳解决方案.

This is also the best solution we've found for android.

这篇关于是否可以使用AVPlayer缓存HLS段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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