小程序播放同一音频两次要怎么写?
发布于 6 年前 作者 cwu 6732 次浏览 来自 问答
        innerAudioContext.src = ""
        innerAudioContext.onEnded(() => {
          innerAudioContext.onPlay(() => {
            innerAudioContext.offEnded()
          })
          innerAudioContext.play()
        })
 

        innerAudioContext.play()



目前是上面这么写的,工具端没问题。请问还有别的写法吗?

问完发现应该追求更高一点,譬如封装成函数,输入播放次数和源地址数组然后调用?

或者loop参数是否应该传入boolean或者数值呢?

function playTwice(srcs,maxtimes) {
  let secondIAC = wx.createInnerAudioContext()
  secondIAC.obeyMuteSwitch = false
  secondIAC.onError(() => {
    showToast({
      icon: 'none',
      title: '发音加载失败',
    })
  })
  let times = 0
  secondIAC.src = src[times]
  secondIAC.onPlay(() => {
    times++
  })
  secondIAC.onEnded(() => {
    if (times === maxtimes) {
      secondIAC.destroy()

    }

    secondIAC.src = src[times]

    secondIAC.play()
  })
  secondIAC.play()
}

回到顶部