Pyttsx在OSX上讲话后挂在runAndWait()中 [英] Pyttsx hangs in runAndWait() after speaking on OSX

查看:533
本文介绍了Pyttsx在OSX上讲话后挂在runAndWait()中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在研究将Python 2.7用于我正在做的项目中的Pyttsx,但是说完之后,程序会挂起,不会继续.

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.

这是我的代码:

import pyttsx

engine = pyttsx.init()

engine.say("Hello world.")
engine.runAndWait()

运行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.

这对其他任何人来说都是常见的吗?有没有解决的办法?如果没有,那么有没有很好的选择? (除了Google TTS)

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)

谢谢!

--------- EXTRA -------- 我将Macbook Pro与OS X El Capitan 10.11.4配合使用

---------EXTRA-------- I use a Macbook Pro with OS X El Capitan 10.11.4

推荐答案

要解决此问题, 1-为pyttsx3创建一个类; 2-创建该类的实例,将文本发送给该类,然后对它进行del()处理. 3-重复步骤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.

课程:

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()

实例:

 tts = _TTS()
 tts.start("text")
 del(tts)

这篇关于Pyttsx在OSX上讲话后挂在runAndWait()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆