pyttsx3 icon indicating copy to clipboard operation
pyttsx3 copied to clipboard

Engine Hangs After Speaking

Open jsl303 opened this issue 4 years ago • 8 comments

The following test hangs after speaking 0.

import pyttsx3

def speak(msg): engine.say(msg) engine.runAndWait()

engine = pyttsx3.init() for i in range(3): speak(str(i))

jsl303 avatar Jun 28 '20 02:06 jsl303

probably the method runAndWait() as it indicate it should run, speak then wait for few moments

Mishalovoper avatar Jul 01 '20 17:07 Mishalovoper

@Mishalovoper, thanks for the suggestion, but it's the same even if I put time.sleep between call.

jsl303 avatar Jul 07 '20 03:07 jsl303

Which os are you using? Which version of pyttsx3 are you using?

nateshmbhat avatar Jul 07 '20 03:07 nateshmbhat

Raspberry Pi 4: 2020-05-27-raspios-buster-full-armhf pyttsx3: 2.88 Bluealsa: 1.4.0

jsl303 avatar Jul 07 '20 10:07 jsl303

For me it is stucks as well on OSX and it has to do with NSRunLoop of Foundation, but couldn't find much into it - it seems on the 2nd call it will never call stopper.stop

https://github.com/ronaldoussoren/pyobjc/blob/4f77802f2a71a2e7660ce5e53fe608d42aae0e26/pyobjc-framework-Cocoa/Lib/PyObjCTools/AppHelper.py#L262-L263

One dirty workaround is to reset the state of pyttsx3 by reimporting it on every run

import pyttsx3
import importlib

for words in words_to_say:
    importlib.reload(pyttsx3)
    engine = pyttsx3.init()
    engine.save_to_file(words, f'{words}.mp3'))
    engine.runAndWait()

capital-G avatar Sep 27 '20 11:09 capital-G

@capital-G Thanks dude...I searched for a whole day but i couldn't find a solution to it.Your answer helped me.But i have something more to ask too... After fixing that error i get another error:

Traceback (most recent call last):
  File "/home/bo/.local/lib/python3.9/site-packages/pyttsx3/drivers/espeak.py", line 171, in _onSynth
    self._proxy.notify('finished-utterance', completed=True)
ReferenceError: weakly-referenced object no longer exists

could you help with this if you ever faced this...Can't find a stackoverflow answer either.

Ajay-Singh-Rana avatar Oct 10 '21 12:10 Ajay-Singh-Rana

I had the same problem as @Ajay-Singh-Rana . My workaround uses an auxiliary function and that seems to finally do the trick.

import pyttsx3
import importlib

def engine_init ():
    importlib.reload(pyttsx3) # Workaround to be avoid pyttsx3 being stuck
    engine = pyttsx3.init()
    return engine

words_to_say = [ "one", "two", "three" ]

for words in words_to_say:
    importlib.reload(pyttsx3)
    engine = engine_init()
    engine.save_to_file(words, f'{words}.mp3')
    engine.runAndWait()

ruckard avatar May 28 '22 23:05 ruckard

Hi @ruckard, your workaround works for me on MacOS X. I am unsure about the root cause of the issue so would be great if someone could chip in to explain?

AnthonyLe93 avatar Nov 08 '23 16:11 AnthonyLe93