2017-01-13 15 views
0

SFSpeechRecognizerを使用しようとしていますが、正しく実装しているかどうかをテストする方法がなく、比較的新しいクラスがサンプルコードを見つけることができませんでした(私はすぐにわかりません) 。私は何も許されないミスをしている/何かを逃していますか?SFSpeechRecognizerを正しく使用するには?

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

答えて

1

- 私はあまりにもしようとしたが、すべてのSFSpeechRecognizerとSFSpeechAudioBufferRecognitionRequestは同じではありません後に、このコードは、私の作品、私は(haven'tがテスト)あなたは異なるアクセス許可を求める必要はあり(あなたが求めていると思いますマイクとspeechRecognitionを使用する前に?コードはここにあります:

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

これはあらかじめ記録されたファイルでのみ動作しますか? – Spartacus9

+0

はい、次のコード行で設定する必要があります。 [myDir stringByAppendingPathComponent:@ "/ sound.m4a"]; これはオーディオファイルのURLの文字列です。ホームディレクトリから設定する必要があります – Enrique

関連する問題