成都网站制作公司,建筑设计招标网站,name域名注册,网站改版 更换域名官方文档链接#xff1a;https://mp.weixin.qq.com/wxopen/plugindevdoc?appidwx069ba97219f66d99token370941954langzh_CN#- 要使用插件需要先在小程序管理后台的设置-第三方设置-插件管理中添加插件#xff0c;目前该插件仅认证后的小程序。
语音合成…官方文档链接https://mp.weixin.qq.com/wxopen/plugindevdoc?appidwx069ba97219f66d99token370941954langzh_CN#- 要使用插件需要先在小程序管理后台的设置-第三方设置-插件管理中添加插件目前该插件仅认证后的小程序。
语音合成功能
语音合成支持的语言有 zh_CN中国大陆en_US(英文)。
textToSpeech(obj)
参数说明 1、lang文本语言 zh_CN中国大陆en_US(英文)String类型必填项 2、content需要被翻译的文本内容后台限制1000字节大小String类型必填项 3、success调用成功时触发的回调Function类型 回调结果说明 retcoderetcode 0 时语音合成成功Int类型 origin: 原始文本String类型 filename语音合成返回的语音地址可自行下载使用String类型 expired_time语音合成链接超时时间戳 如1525930552超时后无法播放可使用时间为3小时Int类型。 4、fail调用失败时触发的回调Function类型。 回调结果说明 retcode错误码Int类型 msg错误信息String类型。 错误码说明 -20001 语音合成语言格式出错 -20002 输入的待合成格式不正确 -20003 语音合成内部错误 -20005 网络错误 -40001 接口调用频率达到限制请联系插件开发者
使用
1、注册插件
在app.json中注册插件 “plugins”: { “WechatSI”: { “version”: “0.3.5”, “provider”: “wx069ba97219f66d99” } },
2、在页面中引入插件
//引入插件微信同声传译 const plugin requirePlugin(‘WechatSI’)
3、在上述1、2步骤完成后实现语音合成
// 文字转语音 playTextToVoice(){ //创建内部 audio 上下文 InnerAudioContext 对象。 this.innerAudioContext wx.createInnerAudioContext(); const that this; plugin.textToSpeech({ // 调用插件的方法 lang: ‘zh_CN’, // lang: ‘en_US’, content: ‘欢迎进入语音合成’, success: function (res) { that.playAudio(res.filename); } }); }, // 播报语音 playAudio(e) { this.innerAudioContext.src e; //设置音频地址 this.innerAudioContext.play(); //播放音频 },
4、如果在页面隐藏或卸载时不再播放合成的语音可将其关闭或销毁播放实例
onHide() { this.innerAudioContext.stop(); this.innerAudioContext.destroy(); },
案例实现代码 //引入插件微信同声传译
const plugin requirePlugin(WechatSI)Page({/*** 生命周期函数--监听页面显示*/onShow() {// 播报this.playTextToVoice()},// 文字转语音playTextToVoice(){//创建内部 audio 上下文 InnerAudioContext 对象。this.innerAudioContext wx.createInnerAudioContext();const that this;plugin.textToSpeech({// 调用插件的方法lang: zh_CN,// lang: en_US,content: 欢迎进入语音合成,success: function (res) {that.playAudio(res.filename);}});},// 播报语音playAudio(e) {this.innerAudioContext.src e; //设置音频地址this.innerAudioContext.play(); //播放音频},/*** 生命周期函数--监听页面隐藏*/onHide: function () {this.innerAudioContext this.innerAudioContext.stop();this.innerAudioContext this.innerAudioContext.destroy();},/*** 生命周期函数--监听页面卸载*/onUnload: function () {this.innerAudioContext this.innerAudioContext.stop();this.innerAudioContext this.innerAudioContext.destroy();},
})具体案例代码亦可参考https://gitee.com/mei-ruohan/mini-program-collection/tree/master/pages/texttvoice