How to make Pyttsx3 stop talking / interrupt speech with new speech?
See original GitHub issueI am trying to interrupt pyttsx3 while it is talking to say something else.
I am using pyttsx3 with an external event loop, which works great - except for not being able to stop the speech and interrupt it. Here is some code demonstrating the issue:
import pyttsx3
import time
engine = pyttsx3.init()
engine.startLoop(False)
engine.say("knock knock, who's there")
start = time.time()
while time.time() - start < 1:
engine.iterate()
time.sleep(.01)
engine.stop()
engine.say('interrupting cow!')
while time.time() - start < 10:
engine.iterate()
time.sleep(.01)
engine.endLoop()
Expected behavior: “interrupting cow” is said partway through the “knock knock who’s there” Actual behavior: “knock knock who’s there” is said, and “interrupting cow” is never said
Does anyone have a workaround that would make it possible to interrupt pyttsx3 and have it speak something else? Am I misunderstanding the docs on this / is there a different approved way to interrupt it?
I’ve tried using endLoop and startLoop(false) again to create a new loop, but no luck there - that just causes “interrupting cow” to be said after the sentence, instead of interrupting it.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Related StackOverflow Question
Issue-Label Bot is automatically applying the label
bugto this issue, with a confidence of 0.52. Please mark this comment with 👍 or 👎 to give our bot feedback!Links: app homepage, dashboard and code for this bot.
That is my solution:
I got the decorator function from this https://stackoverflow.com/a/19846691 post. And the idea how to terminate the Process from @z3r0flag.
I had no problems so far with my early implementation within my home assistant. If someone comes up with a more efficient/structured solution i would be very happy if it is shared with me.