AVPlayer-播放时切换流质量 [英] AVPlayer - switching stream quality while playing

查看:106
本文介绍了AVPlayer-播放时切换流质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVPlayer播放youtube视频,对于每个youtube视频ID,我都会检索到两个质量不同的流网址.

I'm using AVPlayer to play youtube videos, for each youtube video id I retrieve a couple of stream urls in different qualities.

我想根据网络状态播放特定的流质量.例如,如果用户使用3G,我想播放质量最低的URL,但是如果用户转到wifi,我想无缝切换到质量更好的流.

I want to play a particular stream quality according to the network state. For example if user is on 3G I want to play the lowest quality URL but if user moves to wifi I want to seamlessly switch to the better quality stream.

这并不是什么新鲜事物,YouTube正在他们的应用程序以及其他许多应用程序中做到这一点.

This is nothing new, youtube is doing that in their app and many others.

因此,我想知道使用AVPlayer进行这种切换的最佳方法是什么,我不希望用户在不暂停视频播放或缓冲的情况下尽可能注意到切换.
有什么建议吗?

So I wonder what is the best way to do this kind of switching with AVPlayer, I don't want the user to notice the switching as possible, without pausing the video playback or buffering.
Any advices?

我不确定youtube服务器是否支持这种功能,或者我是否需要在客户端执行此功能.

I'm not sure if this kind of functionality is supported on the youtube servers or if I need to do it on client side.

推荐答案

您应该看看Apple

You should have a look at the Apple documentation on HTTP live streaming.

实现所需的切换类型的唯一方法是,在文档中谈到的是使用m3u索引文件和包含视频数据的TS文件.

The only way to achieve the type of switching that you want and is talked about in the documentation is the use of m3u index files and TS files containing the video data.

您连接到索引文件并存储其内容,该内容将是多个URL以及带宽要求.请参见此处的示例.然后使用 Reachability 类以检查网络状态并连接到适当的溪流.启动可达性通知程序,并通过更改连接到的流来对事件做出反应.这将导致下载属于流的TS文件并对其进行缓冲以进行回放,从而实现所需的切换类型.

You connect to the index file and store its contents, which will be multiple URL's along with bandwidth requirements. See the examples here. Then use the Reachability class to check network status and connect to the appropriate stream. Start the Reachability notifier and react to events by changing the stream you're connected to. This will cause the TS file that belongs to the stream to be downloaded and buffered for playback, achieving the type of switching you want.

正如我之前所说,缺点是需要使用TS文件.这意味着您必须从Youtube下载视频文件,并使用Apple提供的mediafilesegmenter命令行工具进行准备,并将其存储在FTP服务器上!一点都不理想,但据我所知是唯一的方法.

As I previously said, the drawback is the requirement to use TS files. This would mean you video files would have to be downloaded from Youtube, prepared using the Apple provided mediafilesegmenter command line tool and the stored on an FTP server! Not ideal at all but as far as I'm aware the only way to do this.

这篇关于AVPlayer-播放时切换流质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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