0

so i have some delegate functions and i have set delegate via

[speechRecognizer recognitionTaskWithRequest:recognitionRequest delegate:self];

but my delegate functions are not getting called

Is there something wrong with my recording function?

- (void) startRecording 
{

 audioEngine = [[AVAudioEngine alloc] init];

    if (recognitionTask) {
        NSLog(@"recognition task already running");
        [recognitionTask cancel];
        recognitionTask = nil;
    }

    NSError *error;
    audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
    [audioSession setMode:AVAudioSessionModeMeasurement error:&error];
    [audioSession setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

    inputNode = [self getInputNode];
    [speechRecognizer recognitionTaskWithRequest:recognitionRequest delegate:self];
    recognitionRequest.shouldReportPartialResults = YES;

    recognitionTask = [speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        BOOL isFinal = NO;
        if (result) {
            NSLog(@"RESULT - %@",result.bestTranscription.formattedString);
            isFinal = !result.isFinal;
        }
        if (error) {
            NSLog(@"errror = %@", error.localizedDescription );
        }
    }];
    AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0];
    [inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
        [recognitionRequest appendAudioPCMBuffer:buffer];
    }];

    // Starts the audio engine, i.e. it starts listening.
    [audioEngine prepare];
    [audioEngine startAndReturnError:&error];
    NSLog(@"Say Something, I'm listening new");
}

my delegate functions are

- (void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available {
    NSLog(@"Availability:%d",available);
}
- (void) speechRecognitionDidDetectSpeech:(SFSpeechRecognitionTask *)task
{
    NSLog(@"State: %ld ",(long)task.state);
}
-(void) speechRecognitionTaskFinishedReadingAudio:(SFSpeechRecognitionTask *)task
{
    NSLog(@"State  : %ld ",task.state);
}

-(void) speechRecognitionTask:(SFSpeechRecognitionTask *)task didHypothesizeTranscription:(SFTranscription *)transcription
{
    recognizedText = transcription.formattedString;
    [self stopNoAudioDurationTimer];
    [self startNoAudioDurationTimer];
    NSLog(@"State: %@ ",transcription.segments );
    NSLog(@"State: %@ ",transcription.segments.lastObject );
    NSLog(@"State: %@ ",transcription.segments.lastObject );
    NSLog(@"State: %@ ",transcription.segments.lastObject );
}
//func speechRecognitionDidDetectSpeech(SFSpeechRecognitionTask)
-(void) speechRecognitionTask:(SFSpeechRecognitionTask *)task didFinishRecognition:(SFSpeechRecognitionResult *)recognitionResult
{
    recognizedText = recognitionResult.bestTranscription.formattedString;
}

-(void) speechRecognitionTask:(SFSpeechRecognitionTask *)task didFinishSuccessfully:(BOOL)successfully
{
    [self stopNoAudioDurationTimer];
}

1 Answers1

0

It seems like you are creating two different tasks:

// first one here
[speechRecognizer recognitionTaskWithRequest:recognitionRequest delegate:self];

//second one here
recognitionTask = [speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
    BOOL isFinal = NO;
    if (result) {
        NSLog(@"RESULT - %@",result.bestTranscription.formattedString);
        isFinal = !result.isFinal;
    }
    if (error) {
        NSLog(@"errror = %@", error.localizedDescription );
    }
}];

I think that you should only keep the first declaration, otherwise your delegate methods won't be called.

ulayuno
  • 1
  • 1