优化微信小程序音频播放,实现多音频同时播放,互不干扰。

原理就是创建5个 innerAudioContext实例化对象,轮流调用。(小程序只能同时存在5个音频实例)。
附上demo:
const innerAudioContext1 = wx.createInnerAudioContext(),
innerAudioContext2 = wx.createInnerAudioContext(),
innerAudioContext3 = wx.createInnerAudioContext(),
innerAudioContext4 = wx.createInnerAudioContext(),
innerAudioContext5 = wx.createInnerAudioContext();

Page({
tap(){      //  此函数点击调用
this.vioce();
},
vioce(){
const inner = [ innerAudioContext1, innerAudioContext2 , innerAudioContext3 , innerAudioContext4 , innerAudioContext5];
let filePath = "音频地址";
let index = app.globalData.index;
index++;
if(index> 4){
index = 0
}
app.globalData.index = index
inner[index].src = filePath;
   inner[index].stop()
inner[index].play()

}
})

你可能感兴趣的:(优化微信小程序音频播放,实现多音频同时播放,互不干扰。)