获取和设置离线语音助手模式
读取当前离线语音助手模式,并请求设备能力支持的模式。
前提条件
- 设备已连接,并遵循
OnDeviceVoiceAssistantAPI。 onDeviceVoiceAssistantCapability不为.none。- 目标模式与报告的能力兼容,且不为
.unknown。
API 参考
框架
AIBuds.xcframework
声明
- Swift
- Objective-C
/// 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?)
}/// The protocol for the on-device voice assistant API.
@protocol AIBudsOnDeviceVoiceAssistantAPI <AIBudsDeviceAPI>
/// The current mode of the on-device voice assistant.
@property(nonatomic, readonly) AIBudsOnDeviceVoiceAssistantMode onDeviceVoiceAssistantMode;
/// Sets the mode of the on-device voice assistant.
/// - Parameters:
/// - mode: The desired mode to set.
/// - completion: Called when the operation completes.
/// - success: `YES` when the operation succeeds.
/// - error: Error details on failure; otherwise `nil`.
- (void)setOnDeviceVoiceAssistantMode:(enum AIBudsOnDeviceVoiceAssistantMode)mode
completion:(AIBudsCompletionHandler _Nullable)completion;
@end请参阅 onDeviceVoiceAssistantMode、setOnDeviceVoiceAssistantMode 和 OnDeviceVoiceAssistantMode。
兼容模式
| 能力 | 可展示模式 |
|---|---|
.none | 不显示模式控制 |
.keywordWakeup | .disabled, .basic |
.common | .disabled, .basic, .advanced |
使用示例
- Swift
- Objective-C
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")
}id<AIBudsOnDeviceVoiceAssistantAPI> voiceAssistant =
(id<AIBudsOnDeviceVoiceAssistantAPI>)self.device;
if (![voiceAssistant conformsToProtocol:@protocol(AIBudsOnDeviceVoiceAssistantAPI)]) {
NSLog(@"On-device voice assistant is not supported");
return;
}
NSLog(@"Current mode: %ld", (long)voiceAssistant.onDeviceVoiceAssistantMode);
if (voiceAssistant.onDeviceVoiceAssistantCapability !=
AIBudsOnDeviceVoiceAssistantCapabilityCommon) {
NSLog(@"Advanced mode is not supported");
return;
}
[voiceAssistant
setOnDeviceVoiceAssistantMode:AIBudsOnDeviceVoiceAssistantModeAdvanced
completion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"%@", error.localizedDescription ?: @"Unable to change mode");
return;
}
NSLog(@"Mode changed to advanced");
}];注意事项
.unknown表示状态不可用,不得作为目标模式发送。- 成功回调用于确认请求。应根据设备属性或模式变化代理回调刷新 UI。
.advanced包含通话状态语音控制,仅应对.common能力提供。