简体   繁体   English

使用gTTS说话而不是保存为mp3?

[英]use gTTS to speak rather than saving as mp3?

I am trying to create a voice assistant using, gtts(Google text to speech). 我正在尝试使用gtts(Google文本到语音)创建语音助手。

I tried doing this but for audio output, it was able to write something that first saves the file and then plays it using audio player (in ubuntu), how to make it in the way it just speaks rather than saving it just like it does using engine.say() in pyttsx3 我尝试这样做但是对于音频输出,它能够写出首先保存文件的东西然后使用音频播放器(在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"])

It saves the file and plays it using audio player, I just want to play it directly because it doesn't make sense for the assistant to open player every time to speak something. 它保存文件并使用音频播放器播放,我只是想直接播放它,因为助理每次开讲话都没有意义。

Well, I kinda solved it myself 好吧,我有点自己解决了

"""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")

it works 有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM