在使用AVAssetDownloadTask播放时缓存HLS视频 [英] Cache HLS video while playing with AVAssetDownloadTask

查看:92
本文介绍了在使用AVAssetDownloadTask播放时缓存HLS视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在播放HLS流时对其进行缓存.我一直在关注Apple文档(播放脱机资产"部分):

I am trying cache an HLS stream while I am playing it. I was following the Apple documentation on this (the section Playing Offline Assets):

我实现了以下方法-应该在播放时下载我的HLS流:

I've implemented the following method - which should make my HLS streams download while playing:

func downloadAndPlayAsset(_ asset: AVURLAsset) {
    // Create new AVAssetDownloadTask for the desired asset
    // Passing a nil options value indicates the highest available bitrate should be downloaded
    let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset,
                                                             assetTitle: assetTitle,
                                                             assetArtworkData: nil,
                                                             options: nil)!
    // Start task
    downloadTask.resume()

    // Create standard playback items and begin playback
    let playerItem = AVPlayerItem(asset: downloadTask.urlAsset)
    player = AVPlayer(playerItem: playerItem)
    player.play()
}

问题在于它只是不下载视频,仅播放视频流.我还实现了委托,但随后都没有一个被调用:

The problem is it just doesn't download the video, only plays the stream. I've also implemented the delegates, but then none of them get called:

func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didLoad timeRange: CMTimeRange, totalTimeRangesLoaded loadedTimeRanges: [NSValue], timeRangeExpectedToLoad: CMTimeRange)

func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL)

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)

奇怪的是,我注意到下次打开该应用时, assetDownloadURLSession.getAllTask​​s 返回上次会话中我下载的待处理任务.因此,它不会下载流,但会将它们添加到挂起的队列中.您知道背后的理由是什么吗?我有一个理论,正在下载的流和实时流使用不同的质量,因此确实需要以正确的质量重新下载.因此,我创建了一种仅用于一种质量的HLS流进行测试,但是结果仍然相同.

Rather strangely, I noticed that the next time I open the app, the assetDownloadURLSession.getAllTasks returns pending tasks for my downloads from the last session. So it doesn't download the streams but it does add them to a pending queue. Do you know what would be the rationale behind that? I had a theory that the stream being downloaded and the live stream use different quality so it does need to be redownloaded in the right quality. So I created an HLS stream for testing with just one quality but the result is still the same.

如果我删除了将AVPlayerItem分配给AVPlayer的行,则会开始下载并调用所有委托.因此,下载工作本身就可以完成,但是只要我尝试播放它,它就会停止.

If I remove the lines assigning the AVPlayerItem to AVPlayer the download starts and all the delegates gets called. So the download on its own work, but anytime I try to play it just stops.

感谢帮助!

托马斯

推荐答案

我有一个理论,正在下载的流和实时流使用不同的质量,因此确实需要以正确的质量重新下载.

I had a theory that the stream being downloaded and the live stream use different quality so it does need to be redownloaded in the right quality.

您的理论是正确的.

所以下载工作本身就可以完成,但是只要我尝试播放它,它就会停止.

So the download on its own work, but anytime I try to play it just stops.

下载将推迟到AVPlayerItem终止.尝试调用 player.replaceCurrentItem(nil).

The download will be deferred until the AVPlayerItem is terminated. Try calling player.replaceCurrentItem(nil).

这篇关于在使用AVAssetDownloadTask播放时缓存HLS视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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