简体   繁体   中英

AVPlayer - how to turn CC coming from HTTP Live Streaming video on/off programatically?

I am trying to create a custom video player . I am using SwiftUI (v1 and XCode 11.5) and am trying to allow the user to chose whether to turn on/off CC and also choose the language wanted.

How could one achieve this? What I've tried so far is setting the appliesMediaSelectionCriteriaAutomatically property of the AVPlayer to true . Then the CC are shown if enabled from my iPhone's Accessibility settings.

avPlayer.appliesMediaSelectionCriteriaAutomatically = true

I also tried ignoring system preferences by setting it to false and then using the isClosedCaptionDisplayEnabled property to enable the CC but it didn't achieve my expected effect (isClosedCaptionDisplayEnabled is also deprecated).

avPlayer.appliesMediaSelectionCriteriaAutomatically = false
avPlayer.isClosedCaptionDisplayEnabled = true

However I need them to be shown/hidden programatically (when a button is pressed for example) and the OS' settings about captions should be ignored. Also I would like to choose the language of the CCs. Is this even achievable?

NOTE: The video I am playing is streamed from Vimeo if that matters.

First you should get all avaliable CC from your stream and then select one you need eg:

let asset = player.currentItem?.asset
if let group = asset?.mediaSelectionGroup(forMediaCharacteristic: .legible) {
    let listCC = group.options
    if let firstCC = listCC.first {
        player.currentItem?.select(firstCC, in: group)
    }
}

In case to disable CC simply call:

player.currentItem?.select(nil, in: group)

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