问题
I have recently look into using Pyttsx in Python 2.7 for a project I'm doing, but after it has finished speaking the program hangs and won't continue.
Here is my code:
import pyttsx
engine = pyttsx.init()
engine.say("Hello world.")
engine.runAndWait()
After running engine.runAndWait(), it doesn't run anything else and hangs without returning anything. I tried adding a print afterwards, but it didn't print anything.
Is this common for anyone else, and is there a fix for this? If not then is there a good alternative? (Aside from Google TTS)
Thanks!
---------EXTRA-------- I use a Macbook Pro with OS X El Capitan 10.11.4
回答1:
This is a bug in pyttsx, you have to wait until it is resolved
https://github.com/RapidWareTech/pyttsx/issues/26
回答2:
To deal with this problem, 1- make a class for pyttsx3; 2- make an instance of the class, send the text to it, then del() it. 3- repeat step 2 several times.
the Class:
import pyttsx3
class _TTS:
engine = None
rate = None
def __init__(self):
self.engine = pyttsx3.init()
def start(self,text_):
self.engine.say(text_)
self.engine.runAndWait()
the instance:
tts = _TTS()
tts.start("text")
del(tts)
来源:https://stackoverflow.com/questions/39183796/pyttsx-hangs-in-runandwait-after-speaking-on-osx