How to make Pyttsx3 stop talking / interrupt speech with new speech?

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
issue-label-bot[bot]commented, Aug 21, 2020

Issue-Label Bot is automatically applying the label bug to 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.

0reactions
janthmuellercommented, May 31, 2022

That is my solution:

import multiprocessing
import pyttsx3
import time
from threading import Thread


def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = Thread(target=fn, args=args, kwargs=kwargs)
        thread.start()
        return thread
    return wrapper

def speak(phrase):
    engine = pyttsx3.init()
    engine.say(phrase)
    engine.runAndWait()
    engine.stop()

def stop_speaker():
    global term
    term = True
    t.join()

@threaded
def manage_process(p):
	global term
	while p.is_alive():
		if term:
			p.terminate()
			term = False
		else:
			continue

	
def say(phrase):
	global t
	global term
	term = False
	p = multiprocessing.Process(target=speak, args=(phrase,))
	p.start()
	t = manage_process(p)
		
if __name__ == "__main__":
	say("this process is running right now")
	time.sleep(1)
	stop_speaker()
	say("this process is running right now")
	time.sleep(1.5)
	stop_speaker()

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to stop pyttsx3 speech whenever I want - Stack Overflow
just copy and paste that right above "engine.say('your text')" and the engine will stop the moment you press esc on your keyboard, also...
Read more >
Using pyttsx3 — pyttsx3 2.6 documentation - Read the Docs
After construction, an application uses the engine object to register and unregister event callbacks; produce and stop speech; get and set speech engine ......
Read more >
pyttsx3 Documentation - Read the Docs
drivers module. After construction, an application uses the engine object to register and unregister event callbacks; produce and stop speech; ...
Read more >
Controlling text-to-speech pauses with pyttsx3 - Python Forum
Currently, the time between each word is too long (over a second), even with time.sleep set to 0. Why does the .
Read more >
voice assistant using python | The Search Engine You Control
This isn't possible with the speech recognition you are using. This speech recognition takes input and doesn't provide output. With your output system,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found