डिवाइस Voice Assistant Mode पढ़ें और सेट करें
Active on-device voice-assistant mode पढ़ें और device capability द्वारा समर्थित mode का अनुरोध करें।
आवश्यक शर्तें
- डिवाइस connected है और
OnDeviceVoiceAssistantAPIको conform करता है। onDeviceVoiceAssistantCapability.noneनहीं है।- Target mode reported capability के compatible है और
.unknownनहीं है।
API संदर्भ
Framework
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;
@endonDeviceVoiceAssistantMode, setOnDeviceVoiceAssistantMode और OnDeviceVoiceAssistantMode देखें।
Compatible मोड
| Capability | दिखाए जाने वाले modes |
|---|---|
.none | Mode controls न दिखाएँ |
.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");
}];ध्यान दें
.unknownunavailable state दिखाता है; इसे target mode के रूप में न भेजें।- Successful callback request स्वीकार होने की पुष्टि करता है। Device property या mode-change delegate callback से UI refresh करें।
.advancedमें call-state voice control शामिल है; इसे केवल.commoncapability पर दिखाएँ।