1、在微信公众平台登录小程序,添加插件微信同声翻译
搜索微信同声翻译点击添加,即可进行开发
代码
index.html
<view>
<button class="showCharacter" bindtap="wordYun">跟我读</button>
</view>
index.js
//index.js
//获取应用实例
const app = getApp()
//引入插件:微信同声传译
const plugin = requirePlugin('WechatSI');
Page({
data: {
content: '你好啊',//内容
},
// 文字转语音
wordYun:function (e) {
var that = this;
var content = this.data.content;
plugin.textToSpeech({
lang: "zh_CN",
tts: true,
content: content,
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: function (e) {
if (this.data.src == '') {
console.log(暂无语音);
return;
}
this.innerAudioContext.src = this.data.src //设置音频地址
this.innerAudioContext.play(); //播放音频
},
)}