3

Tried iOS13.0 and iOS13.1 and still not working, I tried both AVAggregateAssetDownloadTask and AVAssetDownloadURLSession but none of them working. Not any delegate was called to tell me error of finish, and I found downloaded cache was only 25Kb what was not the right size.

The error is:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedDescription=The operation could not be completed, _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundAVAssetDownloadTask <AFDCA3CC-FA49-488B-AB16-C74425345EE4>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(

    "BackgroundAVAssetDownloadTask <AFDCA3CC-FA49-488B-AB16-C74425345EE4>.<1>"

), NSLocalizedFailureReason=An unknown error occurred (-16654)}
Chan Gu
  • 41
  • 5
  • I don't even get any error, when `task.resume()` it just went silence. May I know where your error info from? – saiday Oct 09 '19 at 10:57
  • @saiday either didn't get any error on release version, i think it's just happen on 13 beta1. I'm trying to fix my m3u8 file, check here https://forums.developer.apple.com/thread/121097 – Chan Gu Oct 10 '19 at 01:55
  • thanks, let us know if you found the solution. – saiday Oct 10 '19 at 15:07
  • It was a bug from apple and that has been fixed. – mAc Apr 20 '20 at 08:59

2 Answers2

2

Found out AVAssetDownloadURLSession can only download HLS with master playlist structure which contains codec attribute into EXT-X-STREAM-INF m3u8 meta on iOS 13+.

I have no idea if this is a bug or function restriction. (m3u8 meta have no CODECS attribute can be played with AVFoundation, but can't be downloaded with AVAssetDownloadURLSession)

Anyway, the solution is:

If you have HLS master playlist:

add CODECS attribute into your #EXT-X-STREAM-INF in m3u8 meta. e.g.

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=63701,CODECS="mp4a.40.34"
playlist.m3u8

If you haven't HLS master playlist yet:

You have to make a master playlist even if you're not supporting adaptive streaming.
The master playlist is the only m3u8 which can contain #EXT-X-STREAM-INF hence CODECS attribute.

saiday
  • 1,290
  • 12
  • 25
  • Hi, @saiday, thx for ur reply. I tried change my HLS file to mater playlist and it worked, BUT, yes, another BUT, my cache could only be played 1 time and if I try to play it again, nothing was been loaded and no error, just a black view with nothing. I tried restart my app agian but it still happened. Did u have the same problem? – Chan Gu Oct 24 '19 at 02:26
  • If you can provide your HLS URL I can take a look. I have no issue with downloaded asset playback. – saiday Oct 24 '19 at 09:28
  • Hi @saiday, https://storage.googleapis.com/rfp-banner-dev/guchan/output/smartnews-multi/playlist.m3u8 this is my url and thx for ur kindness – Chan Gu Oct 25 '19 at 03:33
1

So, I found out that the 'AVAssetDownloadTask' had some error in calling delegates in iOS 13 (13.1,13.2.13.3). Finally, in iOS 13.4.1, Apple has fixed this error and now delegates have called after setting delegate and starting the task. Below is what I used to start downloading the m3u8 file from the server and saving it as an Asset to play later offline.

func downloadVideo(_ url: URL) {
    let configuration = URLSessionConfiguration.background(withIdentifier: currentFileName)

    let downloadSession = AVAssetDownloadURLSession(configuration: configuration,
                                                    assetDownloadDelegate: self,
                              delegateQueue: OperationQueue.main)
    // HLS Asset URL
    let asset = AVURLAsset(url: url)

    // Create new AVAssetDownloadTask for the desired asset
    let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset,
                                                             assetTitle: currentFileName,
                                                             assetArtworkData: nil,
                                                             options: nil)
    // Start task and begin download
    downloadTask?.resume()
}

I tried this on iOS 12 and iOS 13.4.1 and it is working as expected. Also, it was already on the Apple Developer Forums here. Hope this helps someone.

mAc
  • 2,434
  • 2
  • 22
  • 39