pyttsx3: can't get pyttsx3 to stop talking mid utterance more than one time

I can get it to stop the first time with this error AttributeError: 'Engine' object has no attribute '_pump' if I comment this out pyttsx3.driver.DriverProxy.setBusy(engine, busy=False) it works but then when I try to do it again it doesn’t work I’ve looked through all the docs I can find but nothing fixed it any thoughts or suggestions would be appreciated. This is the code it’s a smaller part of a bigger project so there’s a lot of extra code.

#!/usr/bin/env python3

#put imports here
import pyttsx3
import _thread	

SpeekOnOff = True
engine = pyttsx3.init()

#use under scores '_' insted of spaces
def quorry():
  return ['stop', 'shutup', 'shut_up']


def f(text):
  if SpeekOnOff == True:
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate-50)
    engine.say(text)
    engine.runAndWait()
  else:
    print(text)

def Speek(text):
  _thread.start_new_thread( f, (text,) )


def stop():
  pyttsx3.engine.Engine.stop(engine)
  pyttsx3.driver.DriverProxy.setBusy(engine, busy=False)



#put the code specific to making your code work with Steve
#exp. removing all the text from the string that isn't needed
def Steve(quorry=''):
  stop()

if __name__ == '__main__':
  while True:
    Speek(input(': '))

About this issue

Most upvoted comments

The onWord() callback doesn’t work, at least not in python 3.8.5. It doesn’t trigger for every word, it triggers once when utterance is finished.

I was having the same issues, trying to thread the engines loop has been tricky. It seems that if you try to use the engine.connect method and thread the engine, the engine can’t find the callback function it needs.

Here is how I worked around it:

import pyttsx3
import logging
from time import sleep
from multiprocessing.dummy import Process as Thread
#from threading import Thread

logger = logging.getLogger(__name__)

class VoiceBox(object):
    def __init__(self):
        self.t = None
        self._running = False
        self.engine = None

    def _processSpeech(self, text):
        self.engine = pyttsx3.init()
        self.engine.say(str(text))
        self.engine.startLoop(False)
        while self._running:
            self.engine.iterate()
        logger.debug('Thread loop stopped')

    def say(self, text, noInter=2):
        # check if thread is running
        if self.t and self._running:
            logger.debug('Interupting...')
            # stop it if it is
            self.stop()
        # iterate speech in a thread
        logger.debug('Talking: %s', text)
        self.t = Thread(target=self._processSpeech, args=(text,))
        self._running = True
        self.t.daemon = True
        self.t.start()
        # give the thread some space
        # without this sleep and repeatitive calls to 'say'
        # the engine may not close properly and errors will start showing up
        sleep(noInter)

    def stop(self):
        self._running = False
        try:
            self.engine.endLoop()
            logger.debug('Voice loop stopped')
        except:
            pass
        try:
            self.t.join()
            logger.debug('Joined Voice thread')
        except Exception as e:
            logger.exception(e)



if __name__ == '__main__':
    logging.basicConfig()
    logger.setLevel(logging.DEBUG)
    text = '''
    Hobsbawm joined the Communist Party in 1936 and stayed in it for about fifty years. Not only did the cause to which he had devoted his life expire in infamy but the rubbish that it had promised to sweep from the stage-ethnic and national chauvinism-would, in time, make a new bid for legitimacy.
    '''
    text2 = '''
    pepe hands
    '''
    v = VoiceBox()
    v.say(text, 5)
    v.say(text2)
    # I would like to have v.say() know when it is finished talking
    # kill the voice engine and join the talking thread.
    #v.stop()

I put the engine’s iterate loop in a thread using engine.startLoop(False) to start it and engine.endLoop() to stop it (works mid utterance). So now every time you call the VoiceBox.say() method it will stop an utterance if it is currently speaking and say the new utterance.