使用AVURLAssets进行AVPlayer和逐行视频下载 [英] AVPlayer and Progressive Video Downloads with AVURLAssets

查看:183
本文介绍了使用AVURLAssets进行AVPlayer和逐行视频下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一款需要通过 AVPlayer 。文件需要存储在用户的设备上,但也必须在下载时播放。

We've got an app we're working on that needs to provide playback of video files via AVPlayer. The files need to be stored on the user's device, but also must playback while downloading.

目前我们已经构建了一个使用 ASIHTTPRequest 库通过PHP获取视频文件(我们不希望媒体可通过公共URL链接)并写入它们是异步磁盘。

At the moment we've built a download module that uses the ASIHTTPRequest library to get the video files via PHP (we don't want the media to be linkable via public URLs) and write them to disk asynchronously.

然后我们按照 AV Foundation Programming Guide ,使用AVURLAsset获取文件,制作带有资产的AVPlayerItem,使用项目构建AVPlayer,然后将播放器设置为AVPlayerLayer。播放器在完全下载的视频文件中运行良好,并且还可以在模拟器中完美地运行逐渐下载的文件。

We've then setup an AVPlayer as per the AV Foundation Programming Guide, getting the file with AVURLAsset, making an AVPlayerItem with the asset, building the AVPlayer with the item, then setting the player to an AVPlayerLayer. The player runs fine with a fully downloaded video file and will also run a progressively downloaded file perfectly well in the simulator.

不幸的是在实际设备,播放器的行为改变,相反,它似乎加载视频一次,并不试图从磁盘中获取新的数据包。结果是播放器将视频和音频播放到视频中的点,该点标记了在加载资源时的下载进度(例如,如果缓冲了2MB的数据则播放器创建后,播放器最多只能播放2MB的数据)。因为它有视频的标题,播放器会很高兴地继续认为它在视频的整个持续时间内播放,但没有其他视频呈现在屏幕上。

Unfortunately on an actual device, the behavior of the player changes, where instead it seemingly loads the video once and doesn't attempt to grab new packets from disk. The result is that the player will play video and audio up to the point in the video that marks where the download progress was at the time the asset was loaded (e.g. if 2MB of data are buffered then the player is created, the player will only play up to the 2MB worth of data). Because it has the video's header, the player will happily continue thinking it's playing for the full duration of the video, but no other video is rendered to screen.

最后的皱纹如果将视频插入AVComposition并使用该视频创建AVPlayer,则渐进式视频将正常播放。这将是一个很好的解决方案(无论如何我们的应用程序是必要的),除了我们的应用程序的客户端要求视频可以通过AirPlay在Apple TV上播放,AVCompositions无法播放。

The last wrinkle is that if the video is inserted into an AVComposition and the AVPlayer is created with that, the progressive video will play fine. This would be a fine solution (and is necessary for our app anyway on occasion) except that the client for our app requires that the video be playable on an Apple TV via AirPlay, which AVCompositions are incapable of.

有没有人知道是否有办法使用AVURLAssets构建的AVPlayer逐步下载视频文件?有没有办法强制播放器/ playerItem从磁盘读取使用AVURLAsset的方式似乎与AVComposition有关,而不是看似在内存中缓存视频?

Does anyone know if there is a way to play progressively downloading video files using an AVPlayer built from AVURLAssets? Is there a way to force the player/playerItem to read from disk with an AVURLAsset the way it seems to do with an AVComposition instead of seemingly caching the video in memory?

谢谢!

推荐答案

我没有解决方案只是让它与AVURLAssets一起工作,但我使用的方法略有不同。我们将应用程序与 CocoaHTTPServer 捆绑在一起,播放未通过针对本地服务器的HTTP请求完全下载的视频文件。

I haven't a solution to just make it work wit AVURLAssets but I use a slightly different approach. We bundle our App with CocoaHTTPServer and play video files which aren't fully downloaded trough a HTTP request against the local server.

服务器知道文件的总长度,然后可以通过查看HTTP-Headers来确定文件的哪一部分是请求,并从磁盘或远程源加载它。

The server knows the total length of the file and can then decide by looking at the HTTP-Headers which part of the file is request and either loads it from disk or from remote source.

开发时总是有3个初始请求,一个用于文件的前两个字节,一个是文件开头的一个较大的块,另一个是块直接该文件的结尾。这就是为什么总是需要直接从远程服务器加载最后一部分,因为玩家从一开始就需要它。我猜对于本地文件也会发生同样的情况,因此播放器会从文件中加载最后一个字节(这些字节不是最后一个字节)并且不会播放该长度。

While developing this there where always 3 initial requests, one for the first two bytes of the file, one of a larger chunk from the beginning of the file and one chunk directly of the end of the file. That's why it was always needed to load at least the last part directly from the remote server since the player would need it right from the start. I would guess the same happens for local files so the player loads the last bytes from the file (which aren't the right last bytes) and won't play over that length.

您必须通过查看提供的HTTPAsyncFileResponse来创建HTTPConnection的子类并创建自己的HTTPResponse类。

You would have to subclass HTTPConnection and make your own HTTPResponse class by looking at the provided "HTTPAsyncFileResponse".

我希望这能让您了解如何实现此目的用不同的方法。

I hope this gives you an idea how to accomplish this with a different approach.

这篇关于使用AVURLAssets进行AVPlayer和逐行视频下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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