一、index.html
<html>
<head>
<meta name="viewport" content="width=device-width">
<style type="text/css">
#vedioDiv {
visibility: hidden;
}
</style>
<script type="text/javascript" src="js/jquery-3.1.1.js"></script>
</head>
<body style="margin: 0px;">
<lable>请输入文字:</lable>
<input id="voiceText" type="text" />
<button id="voiceTextBtn">转化为语音</button>
<div id="vedioDiv">
</div>
<script type="text/javascript" src="js/test_view.js"></script>
<script type="text/javascript" src="js/test_model.js"></script>
<script type="text/javascript" src="js/test_controller.js"></script>
</body>
</html>
二、test_model.js(核心)
参数 | 选择 | 描述 |
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为情感合成-度丫丫,默认为普通女声 |
var Model = {};
Model.getVoiceByText= function getVoiceByText(vedioJqueryObj , voiceText){
var source = "<source src=http://tsn.baidu.com/text2audio?tex="+voiceText+"&lan=zh&cuid=8d:89:a5:ba:65:0d&"+
"ctp=1&tok=24.2695392b5384125f296447e16fd1f16d.2592000.1517986880.282335-10222819 "+
"type='audio/mp3'>";
var vedio="<video controls='controls' autoplay='autoplay' name='media'>"+source+"</video>"
View.vedioDiv.empty();
View.vedioDiv.html(vedio);
}
三、test_controller.js
$(document).ready(function( ) {
$("#voiceTextBtn").click(function(){
var voiceText = $("#voiceText").val();
Model.getVoiceByText(View.vedioObj,voiceText);
})
})
四、test_view.js
var View = {};
var vedioDiv = $("#vedioDiv");
View.vedioDiv=vedioDiv;
效果图: