I am using AVAssetDownloadURLSession
's makeAssetDownloadTask
method on iOS 15 to simultaneously play and download an HLS VOD stream. Sample code:
@objc func createDownloadSession() {
// Create new background session configuration.
let configuration = URLSessionConfiguration.background(withIdentifier: "TestAppSession")
configuration.timeoutIntervalForResource = 60 * 60 * 2
// Create a new AVAssetDownloadURLSession with background configuration, delegate, and queue
downloadSession = AVAssetDownloadURLSession(configuration: configuration,
assetDownloadDelegate: self,
delegateQueue: OperationQueue.main)
}
@objc func downloadAndCreatePlayerItem(_ url: URL) -> AVPlayerItem? {
// Create new AVAssetDownloadTask for the desired asset
let asset = AVURLAsset(url: url)
// Create the task to download the track.
let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset,
assetTitle: "Test Title",
assetArtworkData: nil,
options: nil)
// Start task
downloadTask!.resume()
// Return the URL asset
return AVPlayerItem(asset: downloadTask!.urlAsset)
}
Everything seems to work fine; it starts pulling the data down at a fast rate, and plays the audio immediately.
However, after a few seconds it stops downloading. It seems to only load around 15 minutes of the HLS audio (I can see the loadedTimeRanges
).
How do I get it to download the entire HLS stream? Is there some throttling I am encountering by the OS? The app is in the foreground, though it is a background session. Anything I'm missing? Thank you in advance!