When using the AVPlayerViewController
, the user is allowed to select whether the subtitles are on a specific language, off
, or set to auto
. Setting the requiresFullSubtitles
property I can force the display of subtitles, but that is not what I want.
Is there a way to detect what the user has selected for the subtitle setting, whether a language is selected, off
, or auto
?
You can grab the currently selected language options & also grab the language info when it's used to set the subtitle or audio track, as described in "Adding Subtitles and Alternative Audio Tracks."
Available subtitles & audio tracks are found in an array of availableMediaCharacteristics
for the video asset.
They're grouped in an AVMediaSelectionGroup
by whether they are AVMediaCharacteristicAudible
or AVMediaCharacteristicLegible
...
The currently selected option is found by:
`func selectedMediaOption(in mediaSelectionGroup: AVMediaSelectionGroup) -> AVMediaSelectionOption?`
It could return nil
so "none," or it would return whatever language is selected. So you could set up some custom 'didChange' listener on that property. Doesn't seem to be any sort of publicly available notification for this, so you'd have to make your own.
Whenever you would select/set the subtitle option on the player, you could capture and use that same information to do whatever it is you intend to do with it:
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristicLegible) {
let locale = Locale(identifier: "es-ES")
let options =
AVMediaSelectionGroup.mediaSelectionOptions(from: group.options, with: locale)
if let option = options.first {
/*** DO WHATEVER YOU WANT HERE AFTER CAPTURING THE LANGUAGE SELECTION & RETRIEVING AN AVAILABLE SUBTITLE ***/
playerItem.select(option, in: group)
}
}