4

I am using SFSpeechRecognizer and noticed this error showing up on iOS >= 13.

This is my code for starting recognition process:

- (void)startRecording:(BOOL)collectPartialResults {    
    NSError *error;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    [audioSession setMode:AVAudioSessionModeMeasurement error:&error];
    [audioSession setActive:YES error:&error];

    AVAudioInputNode *inputNode = _audioEngine.inputNode;

    _recognitionRequest = [SFSpeechAudioBufferRecognitionRequest new];
    _recognitionRequest.shouldReportPartialResults = collectPartialResults;

    _recognitionTask = [_internalRecognizer recognitionTaskWithRequest:_recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
       // ...
    }];

    AVAudioFormat *format = [inputNode outputFormatForBus:0];

    @try {
        [inputNode installTapOnBus:0 bufferSize:1024 format:format block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
            [_recognitionRequest appendAudioPCMBuffer:buffer];
        }];
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception.userInfo);
        [self sendStartRecordingErrorMessage:[NSString stringWithFormat:@"%@", exception.userInfo]];
        return;
    }

    NSError *startError;
    [_audioEngine startAndReturnError:&startError];

    if (startError != nil) {
        [self sendStartRecordingErrorMessage:[NSString stringWithFormat:@"%@", startError.userInfo]];
    }
}

This works for one or two times, but eventually leads to this error:

AVAEInternal.h:109   [AVAudioEngineGraph.mm:1397:Initialize: (err = AUGraphParser::InitializeActiveNodesInInputChain(ThisGraph, *GetInputNode())): error -10851

caused by this line: [_audioEngine startAndReturnError:&startError];

I have no idea what does this error mean. My code is almost the same as official example.

I've tried several things such as:

  • using outputFormatForBus instead of inputFormatForBus for installTapOnBus,
  • using different audioSession mode (AVAudioSessionModeVoicePrompt),
  • calling [audioEngine reset] before starting recoding process
Piotr
  • 1,743
  • 1
  • 26
  • 40

1 Answers1

3

I had the same issue. For me the solution was to use a new AVAudioEngine for each recording.

In your example:

- (void)startRecording:(BOOL)collectPartialResults {
  _audioEngine = [[AVAudioEngine alloc] init];

  // ...
}
lassej
  • 6,256
  • 6
  • 26
  • 34
  • Could you show your exact code for whole process? Now it's giving me this super-clear error: +[AFAggregator logDictationFailedWithError:] Error Domain=kAFAssistantErrorDomain Code=209 "(null)" – Piotr Dec 20 '19 at 20:19
  • Sorry, can't share my code. But `kAFAssistantErrorDomain` looks like it's part of the `Speech` framework and not of `AVFoundation`. So it's probably an unrelated error. – lassej Dec 21 '19 at 09:21
  • I made some findings - it seems you should not start recording unless previous one did not return final results. If I do this way I do not encounter this error. – Piotr Dec 21 '19 at 13:52
  • You should get a medal as I was debugging for few hours and looked all over and only your soimple answer acctually solved my issue. Thanks! – Idan Jan 14 '22 at 13:02