前景:
首先想让小程序退出 音频还在播放只能使用 wx.getBackgroundAudioManager()
手机黑屏之后也就时小程序切到后台之后 onTimeUpdate监听失效了
解决方案
监听小程序切到后台的api wx.onAppHide()
然后在里面放一个定时器,控制音频。具体代码如下
App({
onLaunch: function() {
const func = () => {
console.log('关闭事件 音频暂停')
console.log(pageParams.pageParams.audioIsBuy)
if (pageParams.pageParams.audioIsBuy == false) {
let that = this
console.log('关闭事件 停止音频进入')
//当前播放的秒数
let audio_currentTime = that.globalData.innerAudioContext.currentTime
//剩余的秒数
console.log(audio_currentTime)
let surplusTime = (15 - parseInt(audio_currentTime)) * 1000
pageParams.pageParams.videoIdPaly = false
console.log(surplusTime)
setTimeout(function () {
console.log('关闭事件 停止音频')
that.globalData.innerAudioContext.stop()
}, surplusTime);
}
}
wx.onAppHide(func)
},
globalData: {
innerAudioContext: wx.getBackgroundAudioManager(),
}
})