我正在尝试编写一个 gTTS python 程序,它将文本转换为语音并接受它被告知的命令,但是当我运行代码时,我根本没有得到任何响应,终端中没有错误,也没有播放声音,我不是确定要做什么,我的程序中没有看到任何错误。我在 MacOS 上使用 Sublime Text。请帮忙!!
from gtts import gTTS
import os
import time
import playsound
import speech_recognition as sr
def speak(text):
tts = gTTS(text=text, Lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
if "hello" in text:
speak("hello, how are you?")
if "What is your name" in text:
speak("My name is John")