跳到主要内容

获取和设置离线语音助手模式

读取当前离线语音助手模式,并请求设备能力支持的模式。

前提条件

  • 设备已连接,并遵循 OnDeviceVoiceAssistantAPI
  • onDeviceVoiceAssistantCapability 不为 .none
  • 目标模式与报告的能力兼容,且不为 .unknown

API 参考

框架

AIBuds.xcframework

声明

Swift
/// The protocol for the on-device voice assistant API.
protocol OnDeviceVoiceAssistantAPI: DeviceAPI {
    /// The current mode of the on-device voice assistant.
    var onDeviceVoiceAssistantMode: OnDeviceVoiceAssistantMode { get }

    /// Sets the mode of the on-device voice assistant.
    /// - Parameters:
    ///   - mode: The desired mode to set.
    ///   - completion: Called when the operation completes.
    ///     - success: `true` when the operation succeeds.
    ///     - error: Error details on failure; otherwise `nil`.
    func setOnDeviceVoiceAssistantMode(_ mode: OnDeviceVoiceAssistantMode,
                                   completion: AIBudsCompletionHandler?)
}

请参阅 onDeviceVoiceAssistantModesetOnDeviceVoiceAssistantModeOnDeviceVoiceAssistantMode

兼容模式

能力可展示模式
.none不显示模式控制
.keywordWakeup.disabled, .basic
.common.disabled, .basic, .advanced

使用示例

Swift
guard let voiceAssistant = device as? OnDeviceVoiceAssistantAPI else {
    print("On-device voice assistant is not supported")
    return
}

print("Current mode: \(voiceAssistant.onDeviceVoiceAssistantMode)")

guard voiceAssistant.onDeviceVoiceAssistantCapability == .common else {
    print("Advanced mode is not supported")
    return
}

voiceAssistant.setOnDeviceVoiceAssistantMode(.advanced) { success, error in
    guard success else {
        print(error?.localizedDescription ?? "Unable to change mode")
        return
    }
    print("Mode changed to advanced")
}

注意事项

  • .unknown 表示状态不可用,不得作为目标模式发送。
  • 成功回调用于确认请求。应根据设备属性或模式变化代理回调刷新 UI。
  • .advanced 包含通话状态语音控制,仅应对 .common 能力提供。