gpt4 book ai didi

multithreading - 如何在python线程中使用pyttsx

转载 作者:行者123 更新时间:2023-12-04 06:49:27 25 4
gpt4 key购买 nike

我正在与 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_'

最佳答案

错误似乎是它不在 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")

关于multithreading - 如何在python线程中使用pyttsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48025641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com