Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVSpeechSynthesizer not stopping

I am using AVSpeechSynthesizer for TTS in my iOS App. But the didFinishSpeechUtterance method is not being called.

this is my code in the .h:

#import <AVFoundation/AVFoundation.h>

@interface EmailViewController : UIViewController <UITextFieldDelegate, AVSpeechSynthesizerDelegate>

@property (nonatomic, strong) AVSpeechSynthesizer* talker;

this is in the .m

- (void) startTalking {
    NSString *hi = [NSString stringWithFormat:@"%@", humanLanguageArray[speaknumber]];
    AVSpeechUtterance* utter = [[AVSpeechUtterance alloc] initWithString:hi];
    utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
    [utter setRate:0.2f];
    if (!self.talker) {
        self.talker = [AVSpeechSynthesizer new];
    }
    self.talker.delegate = self;
    [self.talker speakUtterance:utter];
}
- (void)stopSpeech
{
    AVSpeechSynthesizer *talked = [[AVSpeechSynthesizer alloc] init];
    if([talked isSpeaking]) {
        [talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
        [talked speakUtterance:utterance];
        [talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utteranc
{
    if (speaknumber < (length-1)) {
        speaknumber += 1;
        self.talker.delegate = self;
        NSString *hi = [NSString stringWithFormat:@"%@", humanLanguageArray[speaknumber]];
        AVSpeechUtterance *utter = [[AVSpeechUtterance alloc] initWithString:hi];
        utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
        [utter setRate:0.2f];
        if (!self.talker) {
            self.talker = [AVSpeechSynthesizer new];
        }
        [self.talker speakUtterance:utter];
    } else if (speaknumber >= length) {
    }
}

If I call stopSpeech, it doesn't stop talking, it keeps talking until speaknumber >= length. Does anyone know how to fix this?

like image 214
Shalin Shah Avatar asked Jan 11 '23 06:01

Shalin Shah


1 Answers

Why are you instantiate AVSpeechSynthesizer in stopSpeech again?

- (void)stopSpeech
{
    AVSpeechSynthesizer *talked = self.talker;
    if([talked isSpeaking]) {
        [talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
        [talked speakUtterance:utterance];
        [talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}

The speaking instance was self.talker, wasn't?

like image 121
Ryan Avatar answered Jan 21 '23 02:01

Ryan