How to download different AVMediaSelections with aggregateAssetDownloadTask?

Hi


I'm using the iOS11 `AVAggregateAssetDownloadTask` API to download an HLS AVAsset with differents subtitles.

Here's the code to create a download task:


downloadTask = self.session.aggregateAssetDownloadTask(with: urlAsset,
     mediaSelections: mediaSelections,
     assetTitle: "my downloads",
     assetArtworkData: nil,
     options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 2000000])


To create the media selection array, I'm duplicating the urlAsset's `preferredMediaSelection` and select the subtitles I want to download.

Here's the code:

let preferredMediaSelection = asset.urlAsset.preferredMediaSelection
var mediaSelections: [AVMediaSelection] = []
if let group = asset.urlAsset.mediaSelectionGroup(forMediaCharacteristic: .legible) {
     group.options.forEach { option in
          guard let mediaSelection = preferredMediaSelection.mutableCopy() as? AVMutableMediaSelection else { return }
          mediaSelection.select(option, in: group)
          mediaSelections.append(mediaSelection)
     }
}


But the session download the whole video for each media selections, instead of downloading the video once, then the french subs, then the english ones, etc...


How to download multiple subtitles (or audio) and the video once?


Thanks

Replies

Hello again.


I switched to the previous API, which is session.makeAssetDownloadTask()


As mentionned in 2016 wwdc video, I download another rendition in the delegate's urlSession(task:didCompleteWithError:) method.


I select the next legible option still not downloaded, and create another task:

let selection = self.getNextLegibleSelection()
let nextTask = self.session.makeAssetDownloadTask(asset: task.urlAsset,
                                                    assetTitle: "my downloads",
                                                    assetArtworkData: .none,
                                                    options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 2000000,
                                                            AVAssetDownloadTaskMediaSelectionKey: selection])


And exactly the same problem as before: the video stream is downloaded again for each now legible option. Since our movies are available with FR/EN/DE/IT/... subtitles, the download duration can be 4 times longer!


Is this the expected behaviour? Am I missing something?


Thanks.