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
- Original URL
- State: open
- Created 5 years ago
- Comments: 15
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:
I put the engine’s
iterate
loop in a thread usingengine.startLoop(False)
to start it andengine.endLoop()
to stop it (works mid utterance). So now every time you call theVoiceBox.say()
method it will stop an utterance if it is currently speaking and say the new utterance.