需要先在微信公众平台下载插件微信同声传译
index.js
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
const app = getApp()
Page({
data: {
content: '您有新的订单',
},
onReady: function () {
this.innerAudioContext = wx.createInnerAudioContext();
this.innerAudioContext.onError(function (res) {
wx.showToast({
title: '语音播放初始化失败',
})
})
},
onLoad: function (options) {
this.readText()
},
readText: function () {
var content = this.data.content;
var that = this;
plugin.textToSpeech({
lang: "zh_CN",
tts: true,
content: content,
success: function (res) {
that.setData({
auidoSrc: res.filename
})
that.readStart();
},
fail: function (res) {
wx.showToast({
title: '语音转换失败',
})
}
})
},
readStart: function () {
this.innerAudioContext.src = this.data.auidoSrc
this.innerAudioContext.play();
},
})
index.html
<button type="warn" bindtap="readText" size="mini">语音阅读</button>
app.json
"plugins": {
"WechatSI": {
"version": "0.3.4",
"provider": "wx069ba97219f66d99"
}
},