Pm Abi
Pm Abi

Reputation: 245

NSURLErrorDomain error -1 When trying to download http Live Streaming Video

I have been trying to download a video from a url using the AVAssetDownloadDelegate. I am able to play the video from the response, but I am unable to download the media using the below code.

I receive this error " The operation couldn’t be completed. (NSURLErrorDomain error -1.) "

This is the code i used

@IBOutlet weak var view1: UIView!

var configuration : URLSessionConfiguration? = nil

var downloadSession : AVAssetDownloadURLSession? = nil

override func viewDidLoad() {
    super.viewDidLoad()


      configuration = URLSessionConfiguration.background(withIdentifier: "downloadIdentifier")


      downloadSession = AVAssetDownloadURLSession(configuration: configuration!,
                                                    assetDownloadDelegate: self,
                                                    delegateQueue: nil)

    let url = URL(string: "http://sample.vodobox.net/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8")
    let asset = AVURLAsset(url: url!)

    let downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset,
                                                              assetTitle: "assetTitle",
                                                              assetArtworkData: nil,
                                                              options: nil)

    // Start task and begin download
    downloadTask?.resume()


    let playerItem = AVPlayerItem(asset: (downloadTask?.urlAsset)!)
    let player = AVPlayer(playerItem: playerItem)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = view1.bounds
    view1.layer.addSublayer(playerLayer)
    player.play()


}

This are the delegates I implemented

func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
    print("Downloaded to Location : \(location)")
}



func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didLoad timeRange: CMTimeRange, totalTimeRangesLoaded loadedTimeRanges: [NSValue], timeRangeExpectedToLoad: CMTimeRange) {
    var percentComplete = 0.0
    // Iterate through the loaded time ranges
    for value in loadedTimeRanges {
        // Unwrap the CMTimeRange from the NSValue
        let loadedTimeRange = value.timeRangeValue
        // Calculate the percentage of the total expected asset duration
        percentComplete += loadedTimeRange.duration.seconds / timeRangeExpectedToLoad.duration.seconds
    }
    print(percentComplete *= 100)
    // Update UI state: post notification, update KVO state, invoke callback, etc.
}

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


    if (error != nil){

        print(error?.localizedDescription)



    }else{

        print("Error")

    }

}

Please have a look at this..

Upvotes: 0

Views: 756

Answers (1)

noc
noc

Reputation: 86

This HLS download API is broken in many different ways. A few thing you can try:

  1. Specify the bitrate in makeAssetDownloadTask call (options parameter)
  2. Modify their sample app to download your video, this could help you debug see if there is anything wrong with your playlist.
  3. implement func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didResolve resolvedMediaSelection: AVMediaSelection) delegate see if it ever gets called.

Upvotes: 2

Related Questions