InnerAudioContext.seek 跳转失效或跳转后 .onTimeUpdate 失效不刷新的问题解决方法
发布于 3 年前 作者 pingyi 250 次浏览 来自 分享

首先说,seek() 跳转成功,但是 .onTimeUpdate 不刷新的问题:

const _InnerAudioContext = wx.createInnerAudioContext()
_InnerAudioContext.autoplay = true
Page({
  data: {
  },
  onLoadfunction (options{
    _InnerAudioContext.src = 'http://sp.9sky.com/convert/song/music/1030028/20210721151849987.mp3'
    _InnerAudioContext.onTimeUpdate(() => {
      console.log(_InnerAudioContext.currentTime);

    })
  },
  forwardfunction () {
    _InnerAudioContext.pause()  // 先暂停
    _InnerAudioContext.seek(31) // 再跳转
    setTimeout(() => {
      _InnerAudioContext.play()
    }, 500);   // 给一个延迟,再播放
  }
})

.seek()跳转失败,音频从头播放的问题:

在具体使用过程中,发现还有一个问题,如果使用的音频所在的服务器__不支持断点续传__,那么 seek() 会从头开始播放。解决方法:

第一种解决方法:配置服务器,支持断点续传;

第二种解决方法:使用小程序云开发的存储,不会存在跳转失败的问题。

第三种解决方法:下载到本地后获取到临时链接,然后播放。

wx.downloadFile({
            url: 'https://www.xxx.com/test/audio.mp3',
            success(res) {
                if (res.statusCode === 200) {
                    _InnerAudioContext.src = res.tempFilePath
                }
            }
        })
回到顶部