跳到主要内容

主动降噪

读取已连接设备当前的 ANC 设置,并更新其模式、增益或淡入淡出行为。

前置条件

  • 设备已连接且就绪。
  • 设备符合 DeviceANCAPI
  • 启用 ANC 控件前,确认 ancMode 不为 .unknown

使用 AI 辅助实现

使用 AI 开发

让 AI 帮助实现此工作流

使用官方“配置 AIBuds 主动降噪”技能,根据你的 App 完成实现。

请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/configure-aibuds-anc,使用该技能在当前 iOS 项目中完成“配置 AIBuds 主动降噪”,并验证结果。
查看官方技能

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds
import AIBudsFoundation

协议

设置和命令由 DeviceANCAPI 定义。

Swift
/// The protocol for device API that supports Active Noise Cancellation (ANC).
protocol DeviceANCAPI: DeviceAPI {
    /// The current Active Noise Cancellation (ANC) mode of the device.
    var ancMode: ANCMode { get }

    /// The current ANC gain value of the device.
    var ancGain: NSNumber? { get }

    /// The current transparency gain value of the device.
    var transparencyGain: NSNumber? { get }

    /// Indicates whether the ANC fade feature is currently enabled.
    var isAncFadeOn: Bool { get }

    /// Sets the Active Noise Cancellation (ANC) mode for the device.
    /// - Parameters:
    ///   - mode: The desired ANC mode to apply.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setAncMode(
        _ mode: ANCMode,
        completion: AIBudsCompletionHandler?
    )

    /// Sets the ANC gain value for the device.
    /// - Parameters:
    ///   - ancGain: The desired ANC gain value to apply.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setAncGain(
        _ ancGain: Int,
        completion: AIBudsCompletionHandler?
    )

    /// Sets the transparency gain value for the device.
    /// - Parameters:
    ///   - transparencyGain: The desired transparency gain value to apply.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setTransparencyGain(
        _ transparencyGain: Int,
        completion: AIBudsCompletionHandler?
    )

    /// Enables or disables the ANC fade feature for the device.
    /// - Parameters:
    ///   - isOn: A Boolean value indicating whether to enable (`true`) or disable (`false`) the ANC fade feature.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setAncFadeOn(
        _ isOn: Bool,
        completion: AIBudsCompletionHandler?
    )
}

属性

属性类型描述
ancModeANCMode当前 ANC 模式。
ancGainNSNumber?当前 ANC 增益;不可用时为 nil
transparencyGainNSNumber?当前通透增益;不可用时为 nil
isAncFadeOnBool / BOOL是否启用 ANC 淡入淡出。

实例方法

方法用途
setAncMode更改 ANC 模式。
setAncGain更改 ANC 增益。
setTransparencyGain更改通透增益。
setAncFadeOn启用或禁用 ANC 淡入淡出。

参数

参数类型描述
modeANCMode / AIBudsANCMode.normal.anc.transparency。不要设置 .unknown
ancGainInt / NSInteger目标 ANC 增益。当前 Demo 使用 0...100;公开 SDK 尚未保证统一范围。
transparencyGainInt / NSInteger目标通透增益。当前 Demo 使用 0...100;公开 SDK 尚未保证统一范围。
isOnBool / BOOL是否启用 ANC 淡入淡出。
completionAIBudsCompletionHandler?可选完成回调。

这些方法不直接返回值。完成回调接收 success 和可选的 NSError

使用示例

读取当前设置

Swift
import AIBuds

guard let device = device as? DeviceANCAPI,
    device.ancMode != .unknown
else {
    print("Device does not support ANC")
    return
}

print("Mode: \(device.ancMode)")
print("ANC gain: \(device.ancGain?.intValue.description ?? "Unavailable")")
print("Transparency gain: \(device.transparencyGain?.intValue.description ?? "Unavailable")")
print("ANC fade: \(device.isAncFadeOn)")

更改 ANC 模式

Swift
device.setAncMode(.anc) { success, error in
    guard success else {
        print("Failed to change ANC mode: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("ANC mode updated")
}

更改增益与淡入淡出

以下值沿用当前 SDK Demo。SDK 发布设备特定的增益限制后,需要重新核对。

Swift
device.setAncGain(60, completion: nil)
device.setTransparencyGain(40, completion: nil)
device.setAncFadeOn(true, completion: nil)

错误处理

命令失败时,将 UI 恢复为设备当前公开的值,并在回调错误可用时向用户报告。公开 API 未定义 ANC 专用错误码。