2017-01-13 2 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

관련 문제