首页 > 解决方案 > 使用 gTTS 说话而不是保存为 mp3?

问题描述

我正在尝试使用 gtts(Google 文本到语音)创建语音助手。

我尝试这样做,但对于音频输出,它能够编写一些东西,首先保存文件,然后使用音频播放器(在 ubuntu 中)播放它,如何以它只是说话的方式制作它,而不是像它那样保存它在 pyttsx3 中使用 engine.say()

def speak(temp):
voice = gTTS(text=temp, lang="en")
voice.save("/home/vikrant/temp.mp3")
opener ="open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, "/home/vikrant/temp.mp3"])

它保存文件并使用音频播放器播放,我只想直接播放,因为助手每次说话都打开播放器没有意义。

标签: pythongoogle-text-to-speechgtts

解决方案


好吧,我自己解决了

"""This function will take a string as input
convert it into voice and save it as a file temp.mp3
play it and delete it"""
def speak(temp):
    voice = gTTS(text=temp, lang="en")
    voice.save("/home/vikrant/temp.mp3")
    opener ="open" if sys.platform == "darwin" else "xdg-open"
    subprocess.call([opener, "/home/vikrant/temp.mp3"])
    print("Speaking.....")
    time.sleep(1)
    os.remove("/home/vikrant/temp.mp3")

有用


推荐阅读