【微信小程序】微信小程序backgroundAudioManager.pause()无法停止播放的原因

由于原先的wx.playBackgroundAudio(Object object)等一系列API停止维护,转到使用BackgroundAudioManager 实例进行操作又出现了不能暂停的情况... ...

 

 

在鼠标按下触发回调函数中,一开始我是这样写的:

onMusicTap: function(event) {
    var isPlaying = this.data.isPlaying
    var cur_postId = this.data.cur_postId
    var postMuisc = postsData.postList[cur_postId].music

    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.title = postMuisc.title
    backgroundAudioManager.coverImgUrl = postMuisc.coverImg
    backgroundAudioManager.src = postMuisc.url

    if (isPlaying) {
      backgroundAudioManager.pause()
      this.setData({
        isPlaying: false
      })
    } else {
      backgroundAudioManager.play()
      this.setData({
        isPlaying: true
      })
    }
  }

 

 

由于看到官方文档上面说“当设置了新的 src 时,会自动开始播放”,我也尝试过把设置src放到else中,但是任然不行。。。

 

最后,

onMusicTap: function(event) {
    var isPlaying = this.data.isPlaying
    var cur_postId = this.data.cur_postId
    var postMuisc = postsData.postList[cur_postId].music

    const backgroundAudioManager = wx.getBackgroundAudioManager()

    if (isPlaying) {
      backgroundAudioManager.pause()
      this.setData({
        isPlaying: false
      })
    } else {
      backgroundAudioManager.title = postMuisc.title
      backgroundAudioManager.coverImgUrl = postMuisc.coverImg
      backgroundAudioManager.src = postMuisc.url
      backgroundAudioManager.play()
      this.setData({
        isPlaying: true
      })
    }
  }

发现其实只要将设置title、coverImgUrl、src写到else括号内就可以成功暂停了。

 

不知道什么原因,不过正常工作了。。。

 

希望有知道原因的大佬能在下方留言解答一下!

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值