模拟机上完好,真机运行总是有最后两秒钟无法播放(还剩2s就触发了停止播放)。求助,推测是小程序时间更新的延迟性导致。SOS
methods: { setMusicMonitor() { const backgroundAudioManager = app.globalData.backgroundAudioManager let that = this let node = this .data.node //点击播放图标和总控开关都会触发这个函数 wx.onBackgroundAudioPlay(e => { console.log( 'playing...' ) node.isPlayingMusic ? null : that.onMusicTap() }) wx.onBackgroundAudioPause(e => { console.log( 'pausing...' ) node.isPlayingMusic ? that.onMusicTap() : null }) wx.onBackgroundAudioStop(e => { if (app.globalData.g_trackAudioProgress.get(node.id)) { // 循环播放 TODO: if (app.globalData.g_trackAudioProgress.get(node.id).stopPoint >= app.globalData.g_trackAudioProgress.get(node.id).duration - 3) { app.globalData.g_trackAudioProgress.set(node.id, { duration: backgroundAudioManager.duration, stopPoint: 0 }) } } console.log( 'stopping...' , app.globalData.g_trackAudioProgress.get(node.id)) node.isPlayingMusic ? that.onMusicTap() : null }) }, onMusicTap(event) { let that = this let node = this .data.node let isPlayingMusic = node.isPlayingMusic // let audioProgress = app.globalData.g_trackAudioProgress.get(node.id) const backgroundAudioManager = app.globalData.backgroundAudioManager node.isPlayingMusic = this .data.node.isPlayingMusic node.stopPoint = processTotalDuration( this .data.node.listened) backgroundAudioManager.onTimeUpdate(e => { that.setData({ duration: processTotalDuration(backgroundAudioManager.duration), currentTime: processTotalDuration(backgroundAudioManager.currentTime), percent: parseInt(100 * (backgroundAudioManager.currentTime / backgroundAudioManager.duration)) }) app.globalData.g_trackAudioProgress.set(node.id, { duration: backgroundAudioManager.duration, stopPoint: backgroundAudioManager.currentTime }) if (app.globalData.g_trackAudioProgress.get(node.id)) { // 循环播放 TODO: if (app.globalData.g_trackAudioProgress.get(node.id).stopPoint >= (app.globalData.g_trackAudioProgress.get(node.id).duration) - 3) { app.globalData.g_trackAudioProgress.set(node.id, { duration: backgroundAudioManager.duration, stopPoint: 0, }) } } console.log(app.globalData.g_trackAudioProgress.get(node.id)) }) // 穿透:改变数据源(course-text)里 this .triggerEvent( 'customevent' , node, { bubbles: true }) // 再加事件绑定 this .setMusicMonitor() if (isPlayingMusic) { // 暂停回调 backgroundAudioManager.pause(e => { console.log( 'will plause' ) }) } else { backgroundAudioManager.src = `${node.content}` backgroundAudioManager.title = node.title // 首次点击,从node中获取时间,之后从audioProgress获取 backgroundAudioManager.startTime = that.data.isFirstTap ? convertTimeToSeconds(that.data.node.listened) : app.globalData.g_trackAudioProgress.get(node.id) && app.globalData.g_trackAudioProgress.get(node.id).stopPoint that.setData({ isFirstTap: false }) console.log( 'will play' ) } }, } |
楼主最后2S无法播放的问题解决了吗?遇到了同样的问题,,,
请问下楼主是怎么使用wx.getBackgroundAudioManager()的兼听函数的呢?为什么我两个页面公用一个对象,分别写了兼听函数,
但是如果我在a页面播放,点击跳转到b页面,在回来a页面会导致a的兼听函数无法使用,而是使用b的兼听函数。