Pyttsx hangs in runAndWait() after speaking on OSX

走远了吗. 提交于 2019-12-24 00:28:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!