简体   繁体   中英

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 ?

正在播放信息中心

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 ?

正在播放信息中心

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 ?

正在播放信息中心

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 ?

正在播放信息中心

Swift version of @kotvaska

You can add this observer while configuring session/periodic observers

NotificationCenter.default.addObserver(
    self,
    selector: #selector(didMediaToggle(_:)),
    name: AVPlayerItem.mediaSelectionDidChangeNotification,
    object: nil
)

@objc func didMediaToggle(_ sender: Notification) {
    print("LOGGER: mediaSelectionDidChangeNotification:", sender.description)
    let subtitles = player.currentItem?.asset.mediaSelectionGroup(forMediaCharacteristic: .legible)
    print("LOGGER: mediaSelectionDidChangeNotification: subtitles:", subtitles)
}
/// this will not be called in case of `CC`, aka only `On` and `Off` case

For CC you can give a shot to this notification as CC(Closed Captions) is completely different from Subtitles .

NotificationCenter.default.addObserver(
    self,
    selector: #selector(didCaptionsToggle(_:)),
    name: UIAccessibility.closedCaptioningStatusDidChangeNotification,
    object: nil
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM