简体   繁体   English

AVPlayer - 如何以编程方式打开/关闭来自 HTTP 直播视频的 CC?

[英]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.我正在使用 SwiftUI(v1 和 XCode 11.5)并试图让用户选择是否打开/关闭CC并选择所需的语言。

How could one achieve this?一个人怎么能做到这一点? What I've tried so far is setting the appliesMediaSelectionCriteriaAutomatically property of the AVPlayer to true .到目前为止,我尝试将AVPlayerapplyMediaSelectionCriteriaAutomatically属性设置为true Then the CC are shown if enabled from my iPhone's Accessibility settings.如果从我的 iPhone 的辅助功能设置中启用,则会显示 CC。

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).我还尝试通过将其设置为 false 来忽略系统首选项,然后使用 isClosedCaptionDisplayEnabled 属性启用 CC,但它没有达到我的预期效果(isClosedCaptionDisplayEnabled 也已弃用)。

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.我也想选择 CC 的语言 Is this even achievable?这甚至可以实现吗?

NOTE: The video I am playing is streamed from Vimeo if that matters.注意:如果重要的话,我正在播放的视频是从Vimeo流式传输的。

First you should get all avaliable CC from your stream and then select one you need eg:首先,您应该从 stream 和 select 中获取所有可用的 CC,例如:

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:如果要禁用 CC,只需调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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