首先,我们来说说是什么问题吧
原因呢?其实我也不确定,我稍微思考了几个可能得原因
在切换图中1和2时,直接把实例清掉,然后再重新注册一个实例,简单且粗暴(我另外又偷偷的进行了一项优化)
// 我在page外定义了两个全局属性,注意:这是写在page外的哈
let radioId = 0 // 存储当前播放id
let radioOldList = [] // 存储当前正在播放的语音列表
// 在onLoad里面进行实例化
this.innerAudioContext = wx.createInnerAudioContext();
// 阅读文字,页面上的点击图标
readText: async function (e) {
const { item } = e.currentTarget.dataset
const that = this;
// 判断当前id和上一次存储的id是不是相同
if (item.id == radioId) {
// 相同就调用pause()暂停方法,再次点击会接着之前暂停处播放
that.innerAudioContext.pause()
} else {
// 不相同就调用stop()停止方法
that.innerAudioContext.stop()
// 同时将实例清除
that.innerAudioContext = null
// 重新创建一个实例
that.innerAudioContext = wx.createInnerAudioContext()
}
that.data.questionList.forEach(el => {
if (el.id == item.id) {
el.type = 'pause'
} else {
el.type = 'play'
}
});
that.setData({
questionList: that.data.questionList,
playItem: item
})
// 当前id和上一次存储的id相同时,跳过语音转换步骤,直接使用之前的存储的语音列表
if (item.id == radioId) {
that.readStart(radioOldList, item.id)
return
}
let list = splitStringByLength(item.audiodialogue, 300)
let radioList = list.map(el => {
return new Promise(resolve => {
plugin.textToSpeech({
lang: "zh_CN",
tts: true,
content: el,
success: function (res) {
resolve(res.filename)
},
fail: function (res) {
wx.showToast({
title: '语音转换失败',
})
}
})
})
})
Promise.all(radioList).then(res => {
that.readStart(res, item.id)
})
},
// 开始阅读
readStart: async function (radioList, id) {
const that = this
// 将当前播放的语音列表存储起来
radioOldList = radioList
for (let text of radioList) {
this.innerAudioContext.src = text;
this.innerAudioContext.onPlay(() => {
console.log('开始播放当前片段', 'onPlay');
});
this.innerAudioContext.onError((err) => {
console.error('音频播放出错', err);
});
this.innerAudioContext.onEnded(async () => {
// 如果是最后一个片段,这里可以结束,否则不需要await
if (text === radioList[radioList.length - 1]) {
that.data.questionList.forEach(el => {
if (el.id == id) {
el.type = 'play'
}
})
that.setData({
questionList: that.data.questionList,
})
that.innerAudioContext.stop()
return
}
});
// 将当前播放的id存储起来
radioId = id
// 确保前一个音频播放结束后再播放下一个
await new Promise(resolve => {
this.innerAudioContext.onEnded(resolve);
this.innerAudioContext.play();
});
}
},
存储上一次的播放id和播放列表
readText方法内,判断当前播放的id是否和存储的相同,相同就跳过语音转换步骤
如果不相同,先停止当前的播放并清除实例后,重新创建实例