首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SFSpeechRecognizer的正确方法?

使用SFSpeechRecognizer的正确方法?
EN

Stack Overflow用户
提问于 2017-01-13 10:59:05
回答 1查看 1.9K关注 0票数 1

我正在尝试使用SFSpeechRecognizer,但是我没有方法来测试我是否正确地实现了它,而且由于它是一个相对较新的类,所以我找不到一个示例代码(我不知道它是否正确)。我是不是犯了什么不可原谅的错误/遗漏了什么?

代码语言:javascript
复制
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status){
    if (status == SFSpeechRecognizerAuthorizationStatusAuthorized) {
        SFSpeechRecognizer* recognizer = [[SFSpeechRecognizer alloc] init];
        recognizer.delegate = self;
        SFSpeechAudioBufferRecognitionRequest* request = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
        request.contextualStrings = @[@"data", @"bank", @"databank"];

        SFSpeechRecognitionTask* task = [recognizer recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult* result, NSError* error){
            SFTranscription* transcript = result.bestTranscription;
            NSLog(@"%@", transcript);
        }];
    }
}];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-26 16:36:13

我也在尝试,但这段代码适用于我,毕竟SFSpeechRecognizer和SFSpeechAudioBufferRecognitionRequest是不一样的,所以我认为(还没有测试过)您必须请求不同的权限(您以前请求过权限吗?使用麦克风和speechRecognition?)。好的,这是代码:

代码语言:javascript
复制
//Available over iOS 10, only for maximum 1 minute, need internet connection; can be sourced from an audio recorded file or over the microphone

    NSLocale *local =[[NSLocale alloc] initWithLocaleIdentifier:@"es-MX"];
        speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:local];
        NSString *soundFilePath = [myDir stringByAppendingPathComponent:@"/sound.m4a"];
        NSURL *url = [[NSURL alloc] initFileURLWithPath:soundFilePath];
        if(!speechRecognizer.isAvailable)
            NSLog(@"speechRecognizer is not available, maybe it has no internet connection");
        SFSpeechURLRecognitionRequest *urlRequest = [[SFSpeechURLRecognitionRequest alloc] initWithURL:url];
        urlRequest.shouldReportPartialResults = YES; // YES if animate writting
        [speechRecognizer recognitionTaskWithRequest: urlRequest resultHandler:  ^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error)
        {
    NSString *transcriptText = result.bestTranscription.formattedString;
            if(!error)
            {
        NSLog(@"transcriptText");
            }
        }];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41633036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档