在 iOS 中尝试使用 AVAssetDownloadURLSession 下载 m3u8 视频时出错

Getting Error when trying to download a m3u8 video using AVAssetDownloadURLSession in iOS

正在尝试使用 AVAssetDownloadURLSession 下载 .m3u8 视频文件 (http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8)。当我 运行 Xcode 中的代码时,出现错误:

"Error Domain=AVFoundationErrorDomain Code=-11800 \"The operation could not be completed\" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed}"

。我用过的代码:

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAssetDownloadDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupAssetDownload()
        // Do any additional setup after loading the view, typically from a nib.
    }


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

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

        let url = URL(string: "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")
        // let url = URL(string: "https://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8")
        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
        downloadTask?.resume()
    }

    //MARK: Delegates
    public func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL){
        print("DownloadedLocation:\(location.absoluteString)")
    }

    public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        print("Error")
    }

    public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
        print("Error")
    }

    public func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
        print("Waiting")
    }

    public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
        print("Finihs collecting metrics:")
    }
}

Link 到 Github 回购:https://github.com/dep2k/m3u8download.git

我检查了你的代码,发现你错过了将 NSAllowsArbitraryLoads 键添加到 YES 下的 NSAppTransportSecurity[=18] =] .plist 文件中的字典

尝试并分享结果。

模拟器不支持下载 HLS 流。

你应该使用真实设备。