跳到主要内容

设置均衡器

读取已连接设备提供的均衡器设置,在 UI 中展示,并应用用户选择的设置。

前置条件

  • 设备已连接且就绪。
  • 设备符合 DeviceEqualizerAPI
  • 尽可能使用设备返回的设置。

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds
import AIBudsFoundation

协议

均衡器操作由 DeviceEqualizerAPI 定义。

Swift
/// The protocol for device API that supports equalizer.
protocol DeviceEqualizerAPI: DeviceAPI {
    /// All available preset and custom equalizer settings reported by the device.
    var allEQSettings: [EQSettingModel] { get }

    /// The currently active equalizer setting.
    var eqSetting: EQSettingModel? { get }

    /// Applies the specified equalizer setting to the device.
    /// - Parameters:
    ///   - equalizerSetting: The equalizer configuration to be applied.
    ///   - completion: A closure that is invoked when the operation completes.
    ///     - success: `true` if the setting was successfully applied; otherwise `false`.
    ///     - error: An `NSError` object if an error occurs during the operation; otherwise `nil`.
    func setEqualizer(
        _ equalizerSetting: EQSettingModel,
        completion: AIBudsCompletionHandler?
    )
}

实例方法

将指定的均衡器设置应用到设备。

Swift
/// Applies the specified equalizer setting to the device.
/// - Parameters:
///   - equalizerSetting: The equalizer configuration to be applied.
///   - completion: A closure that is invoked when the operation completes.
///     - success: `true` if the setting was successfully applied; otherwise `false`.
///     - error: An `NSError` object if an error occurs during the operation; otherwise `nil`.
func setEqualizer(
    _ equalizerSetting: EQSettingModel,
    completion: AIBudsCompletionHandler?
)

参数

参数类型描述
equalizerSettingEQSettingModel要应用的设备设置。
completionAIBudsCompletionHandler?操作结束时调用的可选回调。

回调参数:

名称类型描述
successBool / BOOL所选设置是否已应用。
errorNSError?失败详情;成功时为 nil

返回值

该方法不直接返回值,结果通过完成回调提供。

使用示例

Swift
import AIBuds
import AIBudsFoundation

guard let device = device as? DeviceEqualizerAPI else {
    print("Device does not support equalizer")
    return
}

guard let selectedSetting = device.allEQSettings.first else {
    print("The device did not provide an equalizer setting")
    return
}

device.setEqualizer(selectedSetting) { success, error in
    guard success else {
        print("Failed to apply equalizer: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("Equalizer applied: \(selectedSetting.name ?? "Unnamed setting")")
}

自定义设置

使用已连接设备上报的自定义槽位。其 gains.count 提供设备特定的 band 数量,minimumGainmaximumGain 定义 -12...12 dB 的传输范围。customSetting(index:gains:) 将从零开始的槽位映射为从 customModeStart 起始的模式,并在输入无效时返回 nil

Swift
// A returned custom setting identifies a slot and its supported band count.
guard let reportedSetting = device.allEQSettings.first(where: \.isCustom),
    let slot = reportedSetting.customIndex?.intValue
else {
    return
}

// Supply one gain per reported band. Every value must be within -12...12 dB.
let gains = Array(repeating: 0, count: reportedSetting.gains.count)
guard let customSetting = EQSettingModel.customSetting(index: slot, gains: gains) else {
    return
}

device.setEqualizer(customSetting, completion: nil)

defaultBandCount 是内置预设使用的 band 数量。不要用它覆盖已连接设备上报的不同 band 数量。

错误处理

处理 success == false,并在 error 可用时向用户报告。公开 API 未定义均衡器专用错误码。