【问题标题】:How to use pyttsx in a python thread如何在 python 线程中使用 pyttsx
【发布时间】:2018-06-10 01:46:17
【问题描述】:

我正在使用 pyttsx3 进行文字转语音。我意识到我可以在一个线程中使用它(或者我做错了什么)。你知道为什么吗?

代码示例:

from threading import Thread
import pyttsx3

def myfunc():
  engine = pyttsx3.init()
  engine.say("ok")
  engine.runAndWait()

t = Thread(target=myfunc)
t.start()

错误:

 File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "test.py", line 9, in myfunc
    engine.runAndWait() #blocks
  File "/usr/local/lib/python3.6/site-packages/pyttsx3/engine.py", line 188, in runAndWait
    self.proxy.runAndWait()
  File "/usr/local/lib/python3.6/site-packages/pyttsx3/driver.py", line 204, in runAndWait
    self._driver.startLoop()
  File "/usr/local/lib/python3.6/site-packages/pyttsx3/drivers/nsss.py", line 33, in startLoop
    AppHelper.runConsoleEventLoop()
  File "/usr/local/lib/python3.6/site-packages/PyObjCTools/AppHelper.py", line 241, in runConsoleEventLoop
    nextfire = nextfire.earlierDate_(soon)
AttributeError: 'NoneType' object has no attribute 'earlierDate_'

【问题讨论】:

  • 你能在没有线程的情况下运行 pyttsx3 吗?您示例的代码对我来说运行正确。
  • @eyllanesc 真的吗?是的,当然没有线程 pyttsx3 工作正常
  • 有趣。也许它是基于操作系统的:我的不起作用,而且我和 OP 都在使用 Mac(我假设是因为那些看起来像自制路径)。
  • @rassar 是的,我正在使用 Mac 和 Homebrew...
  • 你能推荐任何其他的多平台 tts 吗?

标签: multithreading python-3.x text-to-speech pyttsx


【解决方案1】:

错误似乎是它没有在 osx 上的线程中运行。以下是一些可能效果不错的示例:

如果你只是需要将文字转成语音,可以使用os.system('say %s')

import os
def myfunc():
  os.system('say ok')

gTTS 或 Google 的 TextToSpeech 引擎,支持 64 种语言,包括意大利语。用法:

from gtts import gTTS
import os
tts = gTTS(text='Good morning', lang='it')
tts.save("good.mp3")
os.system("mpg321 good.mp3")

【讨论】:

  • 谢谢,但需要跨平台,必须支持意大利语。
  • 我希望它离线。对不起;)
  • 那么一个离线、跨平台、多语言的文本转语音引擎?不幸的是,pyttsx 可能是您最好的选择。你可以尝试提交一个 GitHub 问题,看看你能得到什么——github.com/nateshmbhat/pyttsx3/issues/new
  • 已经完成了!让我们看看会发生什么,现在我将使用 gTTS。谢谢
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
相关资源
最近更新 更多