본문으로 건너뛰기

이퀄라이저 설정

연결된 기기가 제공하는 이퀄라이저 설정을 읽어 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입니다.

반환 값

이 메서드는 값을 직접 반환하지 않습니다. 결과는 completion 콜백으로 전달됩니다.

사용 예제

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는 기기별 밴드 수를 제공하고 minimumGainmaximumGain-12...12 dB 전송 범위를 정의합니다. customSetting(index:gains:)은 0부터 시작하는 슬롯을 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는 기본 preset에서 사용하는 밴드 수입니다. 연결된 기기가 다른 밴드 수를 보고하면 이 값으로 덮어쓰지 마세요.

오류 처리

success == false를 처리하고 error가 있으면 표시하세요. 공개 API에는 이퀄라이저 전용 오류 코드가 정의되어 있지 않습니다.