收藏
回答

音频播放IOS上最后2s无法播放

问题模块 框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本
API和组件 小程序 Bug backgroundAudioManager 客户端 6.6.6 1.9.9

模拟机上完好,真机运行总是有最后两秒钟无法播放(还剩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')
            }
        },
    }


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') } }, }
最后一次编辑于  2018-05-03
回答关注问题邀请回答
收藏

2 个回答

  • 贺淼淼
    贺淼淼
    2018-05-19

    楼主最后2S无法播放的问题解决了吗?遇到了同样的问题,,,

    2018-05-19
    赞同
    回复 2
    • Elliot
      Elliot
      2018-05-23
      背景音乐管理器触发停止后,UI上强制将播放进行到底。
      2018-05-23
      回复
    • 贺淼淼
      贺淼淼
      2018-05-23回复Elliot
      我目前也是这么做的,,,,,,捂脸.jpg
      2018-05-23
      回复
  • 棉花糖
    棉花糖
    2018-05-16

    请问下楼主是怎么使用wx.getBackgroundAudioManager()的兼听函数的呢?为什么我两个页面公用一个对象,分别写了兼听函数,

    但是如果我在a页面播放,点击跳转到b页面,在回来a页面会导致a的兼听函数无法使用,而是使用b的兼听函数。

    2018-05-16
    赞同
    回复 2
    • Elliot
      Elliot
      2018-05-16
      再仔细看看文档,全局只有一个背景音乐管理器的;可以放全局变量里边
      2018-05-16
      回复
    • 棉花糖
      棉花糖
      2018-05-16回复Elliot
      感谢!这里有个疑问: 我两个页面都需要兼听backgroundaudioManager的onEnded等函数,他们做的不通的业务 只要我从a突然跳转到b,在回退a页面,a页面的兼听函数就被覆盖掉了。 难道只能每次在onShow重写着几个兼听方法吗?
      2018-05-16
      回复