无法弄清HLS下载

时间:2018-07-09 08:01:22

标签: swift swift4 hls

我尝试了以下操作:

func setupAssetDownload() {
    // Create new background session configuration.
    let configuration = URLSessionConfiguration.background(withIdentifier: "123124123152")

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

    let url = URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")// HLS Asset URL
    let asset = AVURLAsset(url: url!)

    // Create new AVAssetDownloadTask for the desired asset
    let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset,
                                                             assetTitle: "assetTitle",
                                                             assetArtworkData: nil,
                                                             options: nil)
    // Start task and begin download
    print(downloadTask.debugDescription)
    downloadTask?.resume()
}

并已实现

func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
    print("didFinishDownloadingTo \(location.relativePath)")
    playOfflineAsset()
}

但是永远不会调用委托方法didFinishDownloadingTo

还添加了didCompleteWithError代表,但没有成功。 我的班级符合AVAssetDownloadDelegate

1 个答案:

答案 0 :(得分:0)

AVAssetDownloadURLSession 始终仅适用于真实设备。因此,在您的情况下,似乎您已在模拟器上对其进行了尝试。

请使用真实设备