uni-app中使用
小程序中使用
app.json
"plugins": {"WechatSI": {"version": "0.3.3",//版本号"provider": "wx069ba97219f66d99" // 插件id}}
页面中
<view class="yuyinWrap"><textarea class='yuyinCon' placeholder='请输入内容' value='{{content}}' bindinput='conInput'></textarea><view class=''><button class="yuyinBtn start" bindtap='wordYun'>开始</button><button class="yuyinBtn" bindtap='end'>结束</button></view></view>
js内容 ```javascript const app = getApp(); //引入插件:微信同声传译 const plugin = requirePlugin(‘WechatSI’);
Page({ /**
* 页面的初始数据*/data: {content: '一、南阳世界月季大观园一期(东园、中园、北园)', //内容src: '', //},onReady(e) {this.innerAudioContext = wx.createInnerAudioContext();this.innerAudioContext.autoplay = true;},// 手动输入内容conInput: function(e) {this.setData({content: e.detail.value,})},// 文字转语音wordYun: function(e) {var that = this;var content = this.data.content;plugin.textToSpeech({lang: "zh_CN",tts: true,content: content.slice(0, 500),success: function(res) {console.log(res);console.log("succ tts", res.filename);that.setData({src: res.filename})that.yuyinPlay();},fail: function(res) {console.log("fail tts", res)}})},//播放语音yuyinPlay(e) {if (this.data.src == '') {console.log(暂无语音);return;}console.log(this.data.src);this.innerAudioContext.src = this.data.src //设置音频地址this.innerAudioContext.play(); //播放音频this.innerAudioContext.onStop(() => {console.log('i am onStop');innerAudioContext.stop();//播放停止,销毁该实例this.innerAudioContext.destroy();})this.innerAudioContext.onPlay(() => {console.log("正在播放")})this.innerAudioContext.onEnded(() => {console.log('i am onEnded');//播放结束,销毁该实例this.innerAudioContext.destroy();console.log('已执行destory()');})this.innerAudioContext.onError((res) => {/* console.log(res.errMsg);console.log(res.errCode); */this.innerAudioContext.destroy();})},// 结束语音end: function(e) {console.log("结束语音")this.innerAudioContext.pause(); //暂停音频},
})
注意事项
字数限制
官网上说内容限制为1000字节,也就是说可以输入将近500中文,但实际测试后发现350个文字会报错,330个文字可能会报错,320个字稳定不报错 text to speech inner server failed
