Downloading multiple HLS audio files simultaneously fails

299 Views Asked by At

I have been trying to multiple HLS audio files together in iOS. The files are encrypted with a key. I need to fetch the key and store it locally for offline use. When I download a small number (2 or 3 files) of files simultaneously it works fine, but if I start downloading 10-15 files simultaneously most of them fails with the error message-

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

I am getting error message from NSURLErrorDomain as well but they are rare. The link of the approach which I am using to fetch the key for offline use is below -

Playing Offline HLS with AES-128 encryption iOS

Any help would be appreciated.

class AudioDownloader {

var productKey: String

var downloadUrl: URL

var downloadSession: AVAssetDownloadURLSession?

var fakeDownloadUrl: URL?

var downloadTask: AVAssetDownloadTask?





func downloadAudio() {



        if downloadSession == nil {



            let configuration = URLSessionConfiguration.background(withIdentifier: self.productKey)

            downloadSession = AVAssetDownloadURLSession(configuration: configuration, assetDownloadDelegate: self, delegateQueue: OperationQueue.main)

            configuration.shouldUseExtendedBackgroundIdleMode = true

            configuration.httpShouldSetCookies = true

            configuration.httpShouldUsePipelining = false

            configuration.allowsCellularAccess = true

            configuration.isDiscretionary = true



        }

            self.fakeDownloadUrl = self.convertToScheme(url: self.downloadUrl, scheme: "fakehttp")



            let asset = AVURLAsset(url: self.fakeDownloadUrl!)



            let loader = asset.resourceLoader

            loader.setDelegate(self, queue: DispatchQueue(label: "dispatch2"))

            self.downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset, assetTitle: "assetTitle \(self.productKey)", assetArtworkData: nil, options: nil)!

            self.downloadTask?.taskDescription = self.productKey

            self.downloadTask?.resume()

    }

}
0

There are 0 best solutions below