官方文檔鏈接:https://mp.weixin.qq.com/wxopen/plugindevdoc?appid=wx069ba97219f66d99&token=370941954&lang=zh_CN#-
要使用插件需要先在小程序管理后臺(tái)的設(shè)置->第三方設(shè)置->插件管理中添加插件,目前該插件僅認(rèn)證后的小程序。
語(yǔ)音合成功能
語(yǔ)音合成支持的語(yǔ)言有 zh_CN(中國(guó)大陸),en_US(英文)。
textToSpeech(obj)
參數(shù)說(shuō)明:
1、lang:文本語(yǔ)言 zh_CN(中國(guó)大陸)en_US(英文),String類(lèi)型,必填項(xiàng);
2、content:需要被翻譯的文本內(nèi)容,后臺(tái)限制1000字節(jié)大小,String類(lèi)型,必填項(xiàng);
3、success:調(diào)用成功時(shí)觸發(fā)的回調(diào),F(xiàn)unction類(lèi)型;
回調(diào)結(jié)果說(shuō)明:
retcode:retcode == 0 時(shí)語(yǔ)音合成成功,Int類(lèi)型;
origin: 原始文本,String類(lèi)型;
filename:語(yǔ)音合成返回的語(yǔ)音地址,可自行下載使用,String類(lèi)型;
expired_time:語(yǔ)音合成鏈接超時(shí)時(shí)間戳 如1525930552,超時(shí)后無(wú)法播放,可使用時(shí)間為3小時(shí),Int類(lèi)型。
4、fail:調(diào)用失敗時(shí)觸發(fā)的回調(diào),F(xiàn)unction類(lèi)型。
回調(diào)結(jié)果說(shuō)明
retcode:錯(cuò)誤碼,Int類(lèi)型;
msg:錯(cuò)誤信息,String類(lèi)型。
錯(cuò)誤碼說(shuō)明
-20001 語(yǔ)音合成語(yǔ)言格式出錯(cuò)
-20002 輸入的待合成格式不正確
-20003 語(yǔ)音合成內(nèi)部錯(cuò)誤
-20005 網(wǎng)絡(luò)錯(cuò)誤
-40001 接口調(diào)用頻率達(dá)到限制,請(qǐng)聯(lián)系插件開(kāi)發(fā)者
使用
1、注冊(cè)插件
在app.json中注冊(cè)插件
“plugins”: {
“WechatSI”: {
“version”: “0.3.5”,
“provider”: “wx069ba97219f66d99”
}
},
2、在頁(yè)面中引入插件
//引入插件:微信同聲傳譯
const plugin = requirePlugin(‘WechatSI’)
3、在上述1、2步驟完成后實(shí)現(xiàn)語(yǔ)音合成
// 文字轉(zhuǎn)語(yǔ)音
playTextToVoice(){
//創(chuàng)建內(nèi)部 audio 上下文 InnerAudioContext 對(duì)象。
this.innerAudioContext = wx.createInnerAudioContext();
const that = this;
plugin.textToSpeech({
// 調(diào)用插件的方法
lang: ‘zh_CN’,
// lang: ‘en_US’,
content: ‘歡迎進(jìn)入語(yǔ)音合成’,
success: function (res) {
that.playAudio(res.filename);
}
});
},
// 播報(bào)語(yǔ)音
playAudio(e) {
this.innerAudioContext.src = e; //設(shè)置音頻地址
this.innerAudioContext.play(); //播放音頻
},
4、如果在頁(yè)面隱藏或卸載時(shí)不再播放合成的語(yǔ)音可將其關(guān)閉或銷(xiāo)毀播放實(shí)例
onHide() {
this.innerAudioContext.stop();
this.innerAudioContext.destroy();
},
案例實(shí)現(xiàn)代碼:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-793066.html
//引入插件:微信同聲傳譯
const plugin = requirePlugin('WechatSI')
Page({
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
*/
onShow() {
// 播報(bào)
this.playTextToVoice()
},
// 文字轉(zhuǎn)語(yǔ)音
playTextToVoice(){
//創(chuàng)建內(nèi)部 audio 上下文 InnerAudioContext 對(duì)象。
this.innerAudioContext = wx.createInnerAudioContext();
const that = this;
plugin.textToSpeech({
// 調(diào)用插件的方法
lang: 'zh_CN',
// lang: 'en_US',
content: '歡迎進(jìn)入語(yǔ)音合成',
success: function (res) {
that.playAudio(res.filename);
}
});
},
// 播報(bào)語(yǔ)音
playAudio(e) {
this.innerAudioContext.src = e; //設(shè)置音頻地址
this.innerAudioContext.play(); //播放音頻
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏
*/
onHide: function () {
this.innerAudioContext && this.innerAudioContext.stop();
this.innerAudioContext && this.innerAudioContext.destroy();
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載
*/
onUnload: function () {
this.innerAudioContext && this.innerAudioContext.stop();
this.innerAudioContext && this.innerAudioContext.destroy();
},
})
具體案例代碼亦可參考:https://gitee.com/mei-ruohan/mini-program-collection/tree/master/pages/texttvoice文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-793066.html
到了這里,關(guān)于小程序中使用微信同聲傳譯插件實(shí)現(xiàn)語(yǔ)音識(shí)別、語(yǔ)音合成、文本翻譯功能----語(yǔ)音合成(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!