百度语音播放

安装  

如果已经安装了pip,执行 pip install baidu-aip 即可。
如果已安装setuptools,执行 python setup.py install 即可。

登录百度ia网站:

用百度账号登录
进入左侧语言应用
创建新应用

 

from aip import AipSpeech

app_id = "16078218"     # 写注册百度语言的账号
api_key = "mc4fLpCV5um1Lta7uZGxhayn"      # 写注册的api的密钥
secret_key = "CDD41zKslm1uEdUFrMlTanaBFwG41kAB"     # 写注册的secret的密钥

def speech(request):
    if request.method == "GET":
        """把文本转为语音,返回mp3格式的音频文件"""
        client = AipSpeech(app_id, api_key, secret_key)
        # synthesis参数: 文本,语言zh(中文),1为pc端,语音{"vol":音量,"spd":语速,"pit":语调,"per":声道(0:女,1:男,2:逍遥音,3:小萝莉)}
        chapter_audio = client.synthesis("蠢逼", "zh", 1, {
                                                            "vol": 5,
                                                            "spd": 4,
                                                            "pit": 5,
                                                            "per": 0
                                                        })
        with open("media/xx.mp3",'wb') as f:   # 生成带有上述 "蠢逼" 的mp3文件
            f.write(chapter_audio)
    return render(request, 'yuyin.html',locals())

前端识别语音进行播放

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
           # 连接mp3文件,自动语音播放
  <audio src="/media/xx.mp3/" controls="controls" style="height: 20px;width: 180px;"></audio>
</body>
</html>

后台文件

猜你喜欢

转载自www.cnblogs.com/liu--huan/p/10795723.html