speech framework integration showing error

2.4k views Asked by At

Am trying to convert the speech to text and display it in UILabel using speech framework. I've authorized the user to allow use microphone.

Here's my code

- (void)startRecording {
if (_recognitionTask != nil) {
[_recognitionTask cancel];
_recognitionTask = nil;
}

NSError *error;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
[audioSession setMode:AVAudioSessionModeMeasurement error:&error];
[audioSession setActive:YES error:&error];

_recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
_recognitionTask = [[SFSpeechRecognitionTask alloc] init];

AVAudioInputNode *inputNode = [_audioEngine inputNode];

_recognitionRequest.shouldReportPartialResults = YES;

_recognitionTask = [_speechRecognizer     recognitionTaskWithRequest:_recognitionRequest resultHandler:^(SFSpeechRecognitionResult *result, NSError  *error) {
BOOL isFinal = NO;

if (result != nil) {
   _textLabel.text = [[result bestTranscription] formattedString];
   isFinal = result.isFinal;
}

NSLog(@"%@", error);

if (error != nil || isFinal) {
  _textLabel.text = [NSString stringWithFormat:@"%@", error];
  [inputNode removeTapOnBus:0];
  [_audioEngine stop];
  _recognitionRequest = nil;
  _recognitionTask = nil;
}
}];

[_audioEngine prepare];
[_audioEngine startAndReturnError:nil];
}

While debugging it comes into the recognitionTaskWithRequest block but the result is nil and am getting error like this:

Error Domain=kAFAssistantErrorDomain Code=203 "Corrupt" UserInfo={NSUnderlyingError=0x14651450 {Error Domain=SiriSpeechErrorDomain Code=102 "(null)"}, NSLocalizedDescription=Corrupt}

1

There are 1 answers

4
Ravi Kiran On

The problem is that this code is missing the code to append buffer of the recognition, so to solve this, Before line [_audioEngine prepare]; add the below code

[_audioEngine.inputNode installTapOnBus:0 bufferSize:1024 format:[inputNode inputFormatForBus:0] block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when){
                //NSLog(@"Tapped");
                [self.recognitionRequest appendAudioPCMBuffer:buffer];
            }];

This solved my issue. Hope it helps you as well.