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

心不动则不痛 提交于 2019-12-02 15:19:36

问题


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.



来源:https://stackoverflow.com/questions/51532165/getting-error-when-trying-to-download-a-m3u8-video-using-avassetdownloadurlsessi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!