利用百度提供的api,把文字合成语音并播放出来。
安装pip install baidu-aip
安装pip install playsound
参考了好几个帖子,一开始没有添加播放插件,找不到合成的音频文件,以为代码有问题,后来才发现音频文件在C:\Users\Administrator 目录中。
from aip import AipSpeech
from playsound import playsound
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis(text = '你好美女,可以交个朋友吗', options={'vol':5})
if not isinstance(result,dict):
with open('audio.mp3','wb') as f:
f.write(result)
playsound('audio.mp3')