我正在使用 Wit.ai、Dialogpt 等在 Python 中构建虚拟助手。我已经编写了 2 个 Python 文件:因为我在 Linux 中tts_windows.py
运行时遇到了问题。我在其中设置了一个条件来检查操作系统,并运行相应的功能:或. 运行时,我的扬声器(笔记本电脑的内置扬声器和耳机)发出静态噪音,超过了实际语音响应的声音。运行时,出现以下错误:tts_linux.py
pyttsx3
main.py
tts_linux()
tts_windows()
tts_windows()
tts_linux()
Error 263 for command:
open welcome.mp3
The specified device is not open or is not recognized by MCI.
Error 263 for command:
close welcome.mp3
The specified device is not open or is not recognized by MCI.
Failed to close the file: welcome.mp3
Traceback (most recent call last):
File "c:\Users\ujana\Documents\MEGAsync\Computer PRojects\DialoGPT-Conversational-AI-Project\main.py", line 138, in <module>
speak_linux(result)
File "c:\Users\ujana\Documents\MEGAsync\Computer PRojects\DialoGPT-Conversational-AI-Project\tts_linux.py", line 18, in speak_linux
playsound("welcome.mp3")
File "C:\Users\ujana\Documents\MEGAsync\Computer PRojects\DialoGPT-Conversational-AI-Project\env\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound))
File "C:\Users\ujana\Documents\MEGAsync\Computer PRojects\DialoGPT-Conversational-AI-Project\env\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 263 for command:
open welcome.mp3
The specified device is not open or is not recognized by MCI.
我应该怎么办?
main.py
调用 TTS 函数的部分:
while True:
q = get_audio()
if q == None:
continue
cprint("USER : {}".format(q), 'green')
result = work(q)
cprint(f"DialoGPT : {result}", 'cyan')
if OS == "linux":
speak_linux(result)
elif OS == "windows":
speak_windows(result)
if "bye" in q:
break
代码tts_windows.py
:
import pyttsx3
engine = pyttsx3.init('sapi5')
def speak_windows(text):
engine.say(text)
engine.runAndWait()
代码tts_linux.py
:
from playsound import playsound
from gtts import gTTS
def speak_linux(text):
myobj = gTTS(text=text, lang='en', slow=False)
myobj.save("sound.mp3")
playsound("sound.mp3")
注意:我的系统中已经安装了音频驱动程序。我已经检查过了。