美文网首页
百度语音合成

百度语音合成

作者: dopaclover | 来源:发表于2017-11-06 11:06 被阅读0次

    一、旧版本百度语音合成
    http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=*****
    直接引用该接口,在text后面加自定义文字的UTF-8编码,在js中可以用encodeURI(文本)编码,将返回的值拼接在text之后即可。

    参数解释:
    lan:固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
    ie:编码方式
    spd:语速,取值0-9,默认为5中语速
    text:合成的文本,使用UTF-8编码。小于512个中文字或者英文数字。(文本在百度服务器内转换为GBK后,长度必须小于1024字节)

    html代码实现:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>百度文字转语音</title>
    </head>
    <body>
    <input type="text" placeholder="请输入要转换的文字" id="test">
    <button onclick="zh()">转换</button>
    </body>
    <script type="text/javascript">
    function zh(){
    var zhText=document.getElementById("test").value;
    zhText = encodeURI(zhText);
    document.write("<audio autoplay="autoplay">");

                document.write("<source src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text="+ zhText +"\" type=\"audio/mpeg\">");
                
                document.write("<embed height=\"0\" width=\"0\" src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text="+ zhText +"\">");
                
                document.write("</audio>");
                
                document.write("<input type=\"text\" placeholder=\"请输入要转换的文字\" id=\"test\">");
                document.write("<button onclick=\"zh()\">转换</button>");
                
                
                document.close("<audio autoplay=\"autoplay\">");
            
                document.close("<source src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text="+ zhText +"\" type=\"audio/mpeg\">");
                
                document.close("<embed height=\"0\" width=\"0\" src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text="+ zhText +"\">");
                
                document.close("</audio>");
                
                document.close("<input type=\"text\" placeholder=\"请输入要转换的文字\" id=\"test\">");
                document.close("<button onclick=\"zh()\">转换</button>");
            }
    

    </script>
    </html>

    二、新版本百度语音合成

    http://tsn.baidu.com/text2audio?tex=*****bd&lan=zh&cuid=&ctp=1&tok=
    上面五个参数是必填的,不然无法执行。

    参数解释:
    tex:合成的文本,使用UTF-8编码。小于512个中文字或者英文数字。(文本在百度服务器内转换为GBK后,长度必须小于1024字节)
    tok:开放平台获取到的开发者access_token
    cuid:用户唯一标识,用来区分用户,计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内
    ctp:客户端类型选择,web端填写固定值1
    lan:固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
    spd:语速,取值0-9,默认为5中语速
    pit:音调,取值0-9,默认为5中语调
    vol:音量,取值0-15,默认为5中音量
    per:发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声

    遇见问题:
    cuid的获取:在电脑上获取Mac,点击运行cmd,执行ipconfig /all 以“以太网 本地连接”的物理地址为准
    tok的获取:进入百度api个人开发的应用管理找到已开通服务并带有语音合成的,查看KEY弹出
    App ID: ****
    API Key:****
    Secret Key: ****
    使用Client Credentials获取Access Token需要应用在其服务端发送请求(推荐用POST方法)到百度OAuth2.0授权服务的“ https://openapi.baidu.com/oauth/2.0/token ”地址上,并带上以下参数:
    grant_type:必须参数,固定为“client_credentials”;
    client_id:必须参数,应用的 API Key;
    client_secret:必须参数,应用的 Secret Key;
    例如:https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHl********LT0vuXV4&client_secret=0rDSjzQ20XUj5i********PQSzr5pVw2&

    将地址放入页面,返回一个json字符串:
    {"access_token":"******","session_key":"9mzdXUWcyJXr7ReyS9dBylQb//n3fV3a7fzDSNpi1Cbhzc6ttriwrvrBXIWW5+alqnD1E816QbRN3hmrSSY1tcxjRqLn8w==","scope":"public audio_tts_post audio_voice_assistant_get wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower","refresh_token":"25.3ef9cb9ab0766ae5bdd3b28fce327712.315360000.1824795213.282335-10057458","session_secret":"ea63cd666c7ac485ba361338f47dd56f","expires_in":2592000}

    access_token就是你想要的了。

    如果合成成功,正常返回为二进制语音文件,具体header信息 Content-type: audio/mp3;

    如果合成出现错误,则会返回json结果,具体header信息为:Content-type: application/json。其中sn字段主要用于DEBUG追查问题,如果出现问题,可以提供帮助确认问题。
    500 不支持输入
    501 输入参数不正确
    502 token验证失败
    503 合成后端错误

    html代码实现:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>百度文字转语音</title>
    </head>
    <body>
    <input type="text" placeholder="请输入要转换的文字" id="test">
    <button onclick="zh()">转换</button>
    </body>
    <script type="text/javascript">
    function zh(){
    var zhText=document.getElementById("test").value;
    zhText = encodeURI(zhText);
    document.write("<audio autoplay="autoplay">");

                document.write("<source src=\"http://tsn.baidu.com/text2audio?lan=zh&cuid=B8-88-E3-8A-6A-B3&ctp=1&tok=24.f1a33e91ad7b3bd146a3d7fb34117065.2592000.1512027213.282335-10057458&per=4&vol=10&tex="+ zhText +"\" type=\"audio/mpeg\">");
                
                document.write("<embed height=\"0\" width=\"0\" src=\"http://tsn.baidu.com/text2audio?lan=zh&cuid=B8-88-E3-8A-6A-B3&ctp=1&tok=24.f1a33e91ad7b3bd146a3d7fb34117065.2592000.1512027213.282335-10057458&per=4&vol=10&tex="+ zhText +"\">");
                
                document.write("</audio>");
                
                document.write("<input type=\"text\" placeholder=\"请输入要转换的文字\" id=\"test\">");
                document.write("<button onclick=\"zh()\">转换</button>");
                
                
                document.close("<audio autoplay=\"autoplay\">");
            
                document.close("<source src=\"http://tsn.baidu.com/text2audio?lan=zh&cuid=B8-88-E3-8A-6A-B3&ctp=1&tok=24.f1a33e91ad7b3bd146a3d7fb34117065.2592000.1512027213.282335-10057458&per=4&vol=10&tex="+ zhText +"\" type=\"audio/mpeg\">");
                
                document.close("<embed height=\"0\" width=\"0\" src=\"http://tsn.baidu.com/text2audio?lan=zh&cuid=B8-88-E3-8A-6A-B3&ctp=1&tok=24.f1a33e91ad7b3bd146a3d7fb34117065.2592000.1512027213.282335-10057458&per=4&vol=10&tex="+ zhText +"\">");
                
                document.close("</audio>");
                
                document.close("<input type=\"text\" placeholder=\"请输入要转换的文字\" id=\"test\">");
                document.close("<button onclick=\"zh()\">转换</button>");
            }
    

    </script>

    </html>

    相关文章

      网友评论

          本文标题:百度语音合成

          本文链接:https://www.haomeiwen.com/subject/jqakmxtx.html