diff --git a/backends/text-to-speech/macosx/macosx-text-to-speech.mm b/backends/text-to-speech/macosx/macosx-text-to-speech.mm index 66feb54d4b6..3f19653408c 100644 --- a/backends/text-to-speech/macosx/macosx-text-to-speech.mm +++ b/backends/text-to-speech/macosx/macosx-text-to-speech.mm @@ -33,20 +33,29 @@ @interface MacOSXTextToSpeechManagerDelegate : NSObject { MacOSXTextToSpeechManager *_ttsManager; + BOOL _ignoreNextFinishedSpeaking; } - (id)initWithManager:(MacOSXTextToSpeechManager*)ttsManager; - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking; +- (void)ignoreNextFinishedSpeaking:(BOOL)ignore; @end @implementation MacOSXTextToSpeechManagerDelegate - (id)initWithManager:(MacOSXTextToSpeechManager*)ttsManager { self = [super init]; _ttsManager = ttsManager; + _ignoreNextFinishedSpeaking = NO; return self; } - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking { - _ttsManager->startNextSpeech(); + if (!_ignoreNextFinishedSpeaking) + _ttsManager->startNextSpeech(); + _ignoreNextFinishedSpeaking = NO; +} + +- (void)ignoreNextFinishedSpeaking:(BOOL)ignore { + _ignoreNextFinishedSpeaking = ignore; } @end @@ -133,9 +142,15 @@ bool MacOSXTextToSpeechManager::startNextSpeech() { bool MacOSXTextToSpeechManager::stop() { _messageQueue.clear(); - _currentSpeech.clear(); // so that it immediately reports that it is no longer speeking - // Stop as soon as possible - [synthesizer stopSpeakingAtBoundary:NSSpeechImmediateBoundary]; + if (isSpeaking()) { + _currentSpeech.clear(); // so that it immediately reports that it is no longer speeking + // Stop as soon as possible + // Also tell the MacOSXTextToSpeechManagerDelegate to ignore the next finishedSpeaking as + // it has already been handled, but we might have started another speach by the time we + // receive it, and we don't want to stop that one. + [synthesizerDelegate ignoreNextFinishedSpeaking:YES]; + [synthesizer stopSpeakingAtBoundary:NSSpeechImmediateBoundary]; + } return true; }