Search code examples
iosmultithreadingavspeechsynthesizer

AVSpeechSynthesizer stutter during UI update


When AVSpeechSynthesizer is in progress of speaking a sentence, and a UI change is happening (for example a ViewController is pushed, a UIButton's title is changed, ...), the AVSpeechSynthesizer is stuttering.

All my UI changes are in a dispatch_async block. I have to do this, because otherwise the UI is not updating correctly.

dispatch_async(dispatch_get_main_queue(), ^{
   [self.aButton setTitle:@"title" forState:UIControlStateNormal];
});

I have tried running speakUtterance on the main thread and on the background thread, but it doesn't matter, the stuttering keeps happening.

If there are no UI changes during the speakUtterance, there is no problem at all.

Is this a bug in AVSpeechSynthesizer? Am I missing something in regards to threading?


Solution

  • Ok, I found out what happened.

    The function that calls the AVSpeechSynthesizer executed this code everytime:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                                 withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionDuckOthers
                                                       error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    

    Turns out this is totally not necessary and will cause the hiccups and stuttering in AVSpeechSynthesizer.

    So it had nothing to do with doing UI stuff during speaking after all.