Shalin Shah
Shalin Shah

Reputation: 8183

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?

Upvotes: 4

Views: 3934

Answers (2)

iOS Lifee
iOS Lifee

Reputation: 2201

Make your AVSpeechSynthesizer's object optional

var synth: AVSpeechSynthesizer?

func stopTextPlay() {
    synth?.stopSpeaking(at: .immediate)
    synth = nil // make AVSpeechSynthesizer's object nil 
}

Upvotes: 0

Ryan
Ryan

Reputation: 4884

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?

Upvotes: 9

Related Questions