后台Java 代码
public void request_baidu_tts_token(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> resultMap = new HashMap<String, Object>();
String BAIDU_APP_ID = "9309";
String BAIDU_API_KEY = "zURXqbSeqqyko2Zm9NAhE8ZNC";
String BAIDU_SECRET_KEY = "m6dK21eTDX6uLpOnMi8PB";
String grant_type = "client_credentials";
String appKey = BAIDU_API_KEY;
String appSecret = BAIDU_SECRET_KEY;
String request_baidu_tts_token_url="https://openapi.baidu.com/oauth/2.0/token?grant_type=" + grant_type
+ "&client_id=" + appKey + "&client_secret=" + appSecret;
try
{
// 得到含有prepay_id的XML
String request_baidu_tts_token_result = HttpUtil.postData(request_baidu_tts_token_url, null);
if(null != request_baidu_tts_token_result)
{
logger.debug("request_openid 返回:" + request_baidu_tts_token_result);
JSONObject jso = JSONObject.parseObject(request_baidu_tts_token_result);
String baidu_tts_token = (String)jso.get("access_token");
if(null != baidu_tts_token)
{
resultMap.put("access_token", baidu_tts_token);
resultMap.put(TcspUtil.RESULT, TcspUtil.RESULT_RC_SUCCESS);
resultMap.put(TcspUtil.MESSAGE, "获取故障信息成功!");
}
else
{
resultMap.put(TcspUtil.RESULT, TcspUtil.RESULT_RC_SUCCESS);
resultMap.put(TcspUtil.MESSAGE, "获取故障信息成功!");
}
}
}
catch (Exception e)
{
logger.error(e);
e.printStackTrace();
resultMap.put(TcspUtils.RESULT, TcspUtils.RESULT_RC_CHECK_FAIL);
resultMap.put(TcspUtils.MESSAGE, "系统异常!");
}
if(null !=response)
TcspJsonUtil.printJson(response, TcspJsonUtil.object2json(resultMap));
}
微信小程序工具
const app = getApp();
function init_baidu_tts() {
app.appformRequest('request_baidu_tts_token',
null,
function (res) {
var val = res.data;
app.globalData.baidu_tts_token = res.data.access_token
},
function (res_fail) {
console.log(res_fail)
}
)
}
// 合成
function baidu_synthesis(msg) {
var text = msg;
var tex = encodeURI(text); //转换编码url_encode UTF8编码
var tok = app.globalData.baidu_tts_token;
var cuid = app.globalData.userId;//IMEI;
var ctp = 1;
var lan = "zh"; // zh表示中文
var filePath = null;
// 字符编码
var spd = 5; // 表示朗读的语速,9代表最快,1是最慢
var url = "https://tsn.baidu.com/text2audio?tex=" + tex + "&lan=" + lan + "&cuid=" + cuid + "&ctp=" + ctp + "&tok=" + tok + "&spd=" + spd + "&aue=3&vol=12"
console.log(url);
wx.downloadFile({
url: url,
success: function (res) {
console.log(res)
var x = res.tempFilePath;
filePath = res.tempFilePath;
play(filePath);
/*
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
if (res.statusCode === 200) {
if (null != filePath)
{
console.log("call play to speak:" + filePath);
play(filePath);
}
}*/
}
})
}
//播放
function play(audioFilePath) {
const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.autoplay = true
innerAudioContext.src = audioFilePath
innerAudioContext.onPlay(() => {
console.log('开始播放')
})
innerAudioContext.onStop(() => {
innerAudioContext.stop()
//播放停止,销毁该实例
innerAudioContext.destroy()
})
innerAudioContext.onEnded(() => {
console.log('i am onEnded')
//播放结束,销毁该实例
innerAudioContext.destroy()
console.log('已执行destory()')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
innerAudioContext.destroy()
})
}
//播放
function speak(msg) {
console.log("call speak to speak:" + msg);
baidu_synthesis(msg);
}
module.exports = {
init_baidu_tts: init_baidu_tts,
baidu_synthesis:baidu_synthesis,
speak: speak
}
然后再所需要的地方调用speak()就ok
来源:CSDN
作者:谜一般的章鱼
链接:https://blog.csdn.net/javaScript_sky/article/details/82253104