I am using AVSpeechSynthesizer for TTS in my iOS application. But the didFinishSpeechUtterance method is not called.
this is my code in .h:
#import <AVFoundation/AVFoundation.h>
@interface EmailViewController : UIViewController <UITextFieldDelegate, AVSpeechSynthesizerDelegate>
@property (nonatomic, strong) AVSpeechSynthesizer* talker;
it's in .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 does not stop talking, it continues to speak until speaknumber >= length. Does anyone know how to fix this?
source
share