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}
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 codeThis solved my issue. Hope it helps you as well.