Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect user selection of subtitles when using AVPlayerViewController

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?

Now Playing Info Center

like image 875
picciano Avatar asked Mar 21 '18 16:03

picciano


3 Answers

mc01's answer is correct, but if you want a cut-and-paste solution for Swift 4, here is what is ended up with:

var selectedSubtitleLocale: Locale?

fileprivate func detectSubtitleLanguage() {
    var locale: Locale?

    if let playerItem = player?.currentItem,
        let group = playerItem.asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic.legible) {
        let selectedOption = playerItem.currentMediaSelection.selectedMediaOption(in: group)
        locale = selectedOption?.locale
    }

    selectedSubtitleLocale = locale
}
like image 62
picciano Avatar answered Nov 02 '22 06:11

picciano


There is a notification AVPlayerItemMediaSelectionDidChangeNotification appeared in iOS 13 and tvOS 13

You can subscribe your AVPlayerItem to this notification:

if (@available(iOS 13.0, tvOS 13.0, *)) {
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleMediaSelectionChange:)
                                             name:AVPlayerItemMediaSelectionDidChangeNotification
                                           object:playerItem];
}

And then you can detect what language or subtitle user has selected:

- (void)handleMediaSelectionChange:(NSNotification *)notification {
    AVPlayerItem *playerItem = (AVPlayerItem *)notification.object;
    if([playerItem.asset isKindOfClass:[AVURLAsset class]]){
        AVURLAsset *asset = (AVURLAsset *)playerItem.asset;
        AVMediaSelectionGroup *audio = [asset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
        AVMediaSelectionGroup *subtitles = [asset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicLegible];
        AVMediaSelectionOption *selectedAudio = [playerItem.currentMediaSelection selectedMediaOptionInMediaSelectionGroup:audio];
        AVMediaSelectionOption *selectedSubtitles = [playerItem.currentMediaSelection selectedMediaOptionInMediaSelectionGroup:subtitles];
    }
}
like image 34
kotvaska Avatar answered Nov 02 '22 08:11

kotvaska


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)
    }
}
like image 32
mc01 Avatar answered Nov 02 '22 08:11

mc01