1
import speech_recognition as sr 
import requests
from gtts import gTTS 
from playsound import playsound
import os
import subprocess

bot_message = ""
message = ""
myobj = gTTS(text="Hello I am Shilpa Sheety Speak Anything I am Listening", lang='en', tld='com.au')
myobj.save("starting.mp3")
playsound("starting.mp3")
while bot_message !="Bye":
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        try:
            message = r.recognize_google(audio)
            print("You said : {}".format(message))
        except:
            print("Sorry Could not recognize your voice") 
    if len(message) == 0:
        continue
    print("Sending Message Now")           
    r = requests.post("http://localhost:5002/webhooks/rest/webhook", json={'message':message})
    print("Bot Says,", end=' ')
    for i in r.json():
        bot_message = i['text']
        print(f"{i['text']}")
        myobj = gTTS(text=bot_message)
        myobj.save("Welcome.mp3")
        playsound("Welcome.mp3")

在上面的程序中,我正在welcome.mp3循环播放。前 2 次迭代运行良好,但在 for 循环的第 3 次迭代中出现以下错误:

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 "Voice_bot.py", line 31, in <module>
    playsound("Welcome.mp3")   File "C:\Users\DJ9004\anaconda4\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))   File "C:\Users\DJ9004\anaconda4\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.*
4

1 回答 1

0

我和你有同样的错误,没有找到答案,我开始做测试。我发现的方法不是很实用,但对我有用。在一个新文件中,我编写了这段代码(例如,我们将调用“function_sound_file”文件):

from playsound import playsound


def function_sound():
    playsound('complete/path/file.wav')

在我遇到问题的文件中,我调用了导入后创建的函数(如下)。

from function_sound_file import function_sound

function_sound()
于 2021-09-27T07:02:09.100 回答