微信小程序之获取百度语音合成

后台Java 代码json

    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));
        
    }微信小程序

微信小程序工具api


 

const app = getApp();服务器

 

function init_baidu_tts() {微信

app.appformRequest('request_baidu_tts_token',app

null,工具

function (res) {post

var val = res.data;ui

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