python - 使用 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"])
它保存文件并使用音频播放器播放,我只想直接播放,因为助手每次说话都打开播放器没有意义。
解决方案
好吧,我自己解决了
"""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")
有用
推荐阅读
- python - 如何在python中中断语音助手
- json - 使用嵌套地图颤振的 API 调用
- reactjs - 无法在 React 功能组件中设置状态
- django - 如何在 Wagtail ( ModelAdmin ) 和 Django Rest API 之间共享数据集
- javascript - 删除动态生成的字段
- c# - StackExchange.Redis 事务链接参数
- c# - 可空属性外键约束中的错误
- django - .sqlite3 数据库的 Dockerfile
- bash - 如何通过管道输入 printf?
- javascript - 调用该函数的每个文本字段的 Javascript 函数