Search code examples
iosobjective-cavspeechsynthesizer

Objective C: Wait for AVSpeechSynthesizer until it finishes speaking a word


I need to disable the interaction with the user until the app finishes speaking. see example code below:

self.view.userInteractionEnabled = NO;
[self speak :@"wait for me to speak"];
self.view.userInteractionEnabled = YES;

-(void)speak:(NSString*)word
{
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:word];
    utterance.rate = AVSpeechUtteranceMinimumSpeechRate;
    utterance.rate = 0.2f;
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage
    [AVSpeechSynthesisVoice currentLanguageCode]];
    [synthesizer speakUtterance:utterance];
}

Solution

  • Use the delegate (AVSpeechSynthesizerDelegate). It tells you when the speech is over, so now enable user interaction again.