简体   繁体   English

正确使用SFSpeechRecognizer的方法?

[英]Proper way to use SFSpeechRecognizer?

I'm trying to use SFSpeechRecognizer but I don't have a way to test if I'm implementing it correctly, and since its a relatively new class i couldn't find a sample code (I don't know swift). 我正在尝试使用SFSpeechRecognizer但是我没有办法测试我是否正确实现了它,并且由于它是一个相对较新的类,因此我无法找到示例代码(我不知道swift)。 Am I making any unforgivable mistakes/missing something ? 我在犯任何无法原谅的错误/遗漏什么吗?

[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);
        }];
    }
}];

I´m trying too but this code works for me, after all SFSpeechRecognizer and SFSpeechAudioBufferRecognitionRequest are not the same, so I think (haven´t tested) you have to ask for different permissions (have you asked for permissions before? to use the microphone and the speechRecognition?). 我也在尝试,但是这段代码对我有用,毕竟所有SFSpeechRecognizer和SFSpeechAudioBufferRecognitionRequest不一样,所以我认为(未经测试)您必须要求不同的权限(之前是否要求过权限?使用麦克风)和语音识别?)。 Ok here´s the code: 好的,这是代码:

//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");
            }
        }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM