Detection of downloaded audios and subtitles HLS

Hello,
I just implemented the feature for downloading the HLS content and the offline playback. However, we offer a custom UI which also provides the possibility to select the subtitles and audios.

My problem is that among this available tracks appears also the audios and subtitles which were not downloaded.

This is the code which I use for downloading the conten:
Code Block swift
let urlAsset = AVURLAsset(url: source.url)
var selectedOptions: [AVMediaSelection] = []
if let subtitles = urlAsset.mediaSelectionGroup(forMediaCharacteristic: .legible),
      let subtitleOption = subtitles.options.first(where: { $0.extendedLanguageTag == preferences.textLanguage }) {
      let mutableMediaSelection = urlAsset.preferredMediaSelection.mutableCopy() as? AVMutableMediaSelection
      mutableMediaSelection?.select(subtitleOption, in: subtitles)
      selectedOptions.append(mutableMediaSelection!)
    }
    if var audios = urlAsset.mediaSelectionGroup(forMediaCharacteristic: .audible),
      let audioOption = audios.options.first(where: { $0.extendedLanguageTag == preferences.audioLanguage }) {
      let mutableMediaSelection = urlAsset.preferredMediaSelection.mutableCopy() as? AVMutableMediaSelection
      mutableMediaSelection?.select(audioOption, in: audios)
      selectedOptions.append(mutableMediaSelection!)
    }
    let mediaSelections: AVMediaSelection = selectedOptions.isEmpty ? urlAsset.preferredMediaSelection : selectedOptions.first!
    guard let task = assetDownloadURLSession.aggregateAssetDownloadTask(with: urlAsset,
                                      mediaSelections: selectedOptions,
                                      assetTitle: source.metadata.title,
                                      assetArtworkData: nil,
                                      options: nil)


It works fine and downloads what I want.
My problem is that I use this code to retrieve the list of available audios and subtitles for the downloaded asset and it keeps returning me all the audios and subtitles (even the ones I did not download):
Code Block swift
let availableSubtitles = playerItem.asset.mediaSelectionGroup(forMediaCharacteristic: .legible)
let availableAudioTracks = playerItem.asset.mediaSelectionGroup(forMediaCharacteristic: .audible)

Is there any way how to filter only the downloaded items? I know that the native AVPlayerViewController does filter them but I can't figure out how.


Answered by RaulBatista in 670091022
Okay I was able to figure it out. Basically I have to make use of AVAssetCache.
Accepted Answer
Okay I was able to figure it out. Basically I have to make use of AVAssetCache.
Detection of downloaded audios and subtitles HLS
 
 
Q