const audioCtx = wx.createWebAudioContext()
const loadAudio = (url) => {
return new Promise((resolve) => {
wx.request({
url,
responseType: 'arraybuffer',
success: res => {
console.log('res.data', res.data)
audioCtx.decodeAudioData(res.data, buffer => {
resolve(buffer)
}, err => {
console.error('decodeAudioData fail', err)
reject()
})
},
fail: res => {
console.error('request fail', res)
reject()
}
})
})
}
const sourceCache = new Set()
const play = () => {
loadAudio('https://downsc.chinaz.net/Files/DownLoad/sound1/202309/y2059.mp3').then(buffer => {
const source = audioCtx.createBufferSource()
source.buffer = buffer
source.connect(audioCtx.destination)
sourceCache.add(source) // Tips:缓存住 source,防止被GC掉,GC掉的话音频会中断
source.onended = () => {
sourceCache.delete(source) // Tips:播放完之后,再清掉source缓存
}
setInterval(()=>{
console.log(audioCtx.state,audioCtx.currentTime)
}, 100)
source.start()
setTimeout(()=>{
source.stop()
}, 100)
}).catch(() => {
console.log('fail')
})
}
play()
wx.createWebAudioContext()一创建就会自动播放,state值一直为running,无法stop
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。