AVSpeechSynthesizer not stopping
ios, ios7, iphone, objective-c, text-to-speech
Solution
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?
Problem
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?