我尝试如下使用语音识别
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self startRecognizer];
}
- (void)startRecognizer {
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
if (status == SFSpeechRecognizerAuthorizationStatusAuthorized)
{
SFSpeechRecognizer *sf =[[SFSpeechRecognizer alloc] init];
NSURL *mp3Path = [[NSBundle bundleForClass:[self class]] URLForResource:@"test" withExtension:@"mp3"];
SFSpeechURLRecognitionRequest *speechRequest = [[SFSpeechURLRecognitionRequest alloc]initWithURL:mp3Path];
[sf recognitionTaskWithRequest:speechRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
NSString * translatedString = [[[result bestTranscription] formattedString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",translatedString);
}];
}
}];
}
最佳答案
在 info.plist
中添加 NSSpeechRecognitionUsageDescription
键,如果您在 simulator
中进行测试,则在设备中运行该项目。你需要运行 ios10
的 ios 真实设备来测试这个!
可以引用hackingwithswift's arcticle了解更多详情!
关于IOS 10 语音识别 API : Error Domain=kAFAssistantErrorDomain Code=1101,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39738990/