You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS Swift:如何检测系统辅助功能「Prefer Closed Caption + SDH」复选框状态?

Checking Closed Captioning Preference in macOS Swift (Cocoa)

Hey there! I've got you covered on this one. On macOS, the equivalent of iOS's UIAccessibility.isClosedCaptioningEnabled isn't a class property—it's a function from the Accessibility framework called AXIsClosedCaptioningEnabled(). This function directly checks if the user has enabled the "Prefer Closed Caption + SDH" option in System Preferences > Accessibility > Audio/Visual.

Step 1: Import the Required Frameworks

First, make sure you import the frameworks needed for accessibility checks and AVPlayer functionality:

import Accessibility
import AVKit

Step 2: Check the System Preference State

Use AXIsClosedCaptioningEnabled() to fetch the current state of the checkbox. It returns a Bool—true if the option is enabled, false otherwise.

func shouldShowClosedCaptions() -> Bool {
    return AXIsClosedCaptioningEnabled()
}

Step 3: Sync the Preference with AVPlayer

To tie this system setting to your AVPlayer instance, you need to set its closedCaptionDisplayEnabled property based on the preference. You'll also want to react if the user changes the setting while your app is running.

Initialize Player with the Preference

let mediaURL = URL(string: "your-media-resource-url")!
let player = AVPlayer(url: mediaURL)

// Enable captions only if the system preference is turned on
player.closedCaptionDisplayEnabled = shouldShowClosedCaptions()

Listen for Preference Changes (Runtime Updates)

Users might toggle the closed caption setting while your app is active. To handle this automatically, register for the AXClosedCaptioningEnabledNotification to update your player's state in real time:

// Register the observer (place this in your setup code, like viewDidLoad or player init)
NotificationCenter.default.addObserver(
    self,
    selector: #selector(captionPreferenceDidUpdate),
    name: NSNotification.Name.AXClosedCaptioningEnabledNotification,
    object: nil
)

// Handle the preference change
@objc private func captionPreferenceDidUpdate() {
    player.closedCaptionDisplayEnabled = AXIsClosedCaptioningEnabled()
}

Don't forget to clean up the observer when it's no longer needed (e.g., in your view controller's deinit):

deinit {
    NotificationCenter.default.removeObserver(
        self,
        name: NSNotification.Name.AXClosedCaptioningEnabledNotification,
        object: nil
    )
}

Why Your Previous Implementation Showed Captions Always

If your captions were always visible before, it's likely because you set player.closedCaptionDisplayEnabled = true unconditionally, without checking the system preference. By binding this property to the result of AXIsClosedCaptioningEnabled(), you'll ensure captions only appear when the user has explicitly enabled the option in System Preferences.

内容的提问来源于stack exchange,提问作者Piyush Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:15:35