Getting Error when trying to download a m3u8 video

2020-04-19 05:00发布

问题:

Trying to download a .m3u8 video file (http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8) using AVAssetDownloadURLSession. When i run the code in Xcode, I get an error:

"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}"

. The code that I have used:

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 to Github repo: https://github.com/dep2k/m3u8download.git

回答1:

I checked your code and found that you missed to add NSAllowsArbitraryLoads key to YES under NSAppTransportSecurity dictionary in your .plist file

Try and share the results.