PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 28, 2022

[FIXED] How to detect user selection of subtitles when using AVPlayerViewController

 September 28, 2022     avplayerviewcontroller, ios, tvos     No comments   

Issue

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


Solution

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)
    }
}


Answered By - mc01
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing