首页 > 解决方案 > 下载 HLS 内容未启动

问题描述

我按照 Apple 的媒体播放编程指南下载 HLS 内容。以下实现通常可以正常工作,但偶尔会在没有明显原因的情况下停止工作。委托方法不会报告任何内容,既不会报告下载进度,也不会报告错误。下载只是拒绝启动。发生这种情况时,要重新开始下载,唯一的方法是重新启动设备。有没有人面临同样的问题?有人可以指点我如何解决这个问题吗?

func setupAssetDownload() {

    // Create new background session configuration.
    let configuration = URLSessionConfiguration.background(withIdentifier: "Player")
    configuration.isDiscretionary = true
    configuration.timeoutIntervalForResource = 300


    // Create a new AVAssetDownloadURLSession with background configuration, delegate, and queue
    downloadSession = AVAssetDownloadURLSession(configuration: configuration,
                                                assetDownloadDelegate: self,
                                                delegateQueue: OperationQueue.main)
}

创建和配置下载会话:

func assetDownload(for item: DownloadItem) {

    let asset = AVURLAsset(url: item.url)

    asset.resourceLoader.preloadsEligibleContentKeys = true
    asset.resourceLoader.setDelegate(self, queue: DispatchQueue.global(qos: .default))


    // Create new AVAssetDownloadTask for the desired asset
    guard let task = downloadSession.makeAssetDownloadTask(asset: asset,
                                                           assetTitle: item.title!,
                                                           assetArtworkData: data,
                                                           options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 2_787_000])
        else {
            status[item.url!] = .error
            return
    }

    task.taskDescription = item.title!

    // Start task and begin download
    task.resume()
}

标签: iosswiftdownloadhttp-live-streaming

解决方案


推荐阅读