收藏
回答

音频mp3播放,开发工具没问题,真机模拟无法获取duration,无法播放

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 需求 InnerAudioContext 客户端 2.6.6 2.6.6

- 需求的场景描述(希望解决的问题)

音频mp3播放,开发工具没问题,真机模拟无法获取duration,无法播放,监听事件没有错

const innerAudioContext = wx.createInnerAudioContext();
page(
onload:(){
   wx.request({
      url: radioUrl,
      method: 'POST',
      data: {
        id: that.data.poetryId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.data.status == 0) {
          console.log("******play******")
          that.setData({
            audiolist: [
              {
                audiosrc: hostUrl + 'static/' +  res.data.data.audio,
                coverimg: ""
              }
            ],
          })
          innerAudioContext.autoplay = true
//音频初始化
          that.Initialization();
        }
      }
    })
}
//音频初始化 函数
  //初始化播放器,获取duration
  Initialization() {
    var t = this;
    if (this.data.audiolist[0].audiosrc.length != 0) {
      //设置src
      innerAudioContext.src = this.data.audiolist[0].audiosrc;
      //运行一次
      innerAudioContext.play()
      innerAudioContext.pause()
      innerAudioContext.onCanplay(() => {
        //初始化duration
        innerAudioContext.duration
        setTimeout(function () {
          //延时获取音频真正的duration
           
          var duration = innerAudioContext.duration;
           
          var min = parseInt(duration / 60);
          var sec = parseInt(duration % 60);
          if (min.toString().length == 1) {
            min = `0${min}`;
          }
          if (sec.toString().length == 1) {
            sec = `0${sec}`;
          }
          t.setData({ audioDuration: innerAudioContext.duration, showTime2: `${min}:${sec}` });
          console.log("&&&&声音长度END" + innerAudioContext.duration)
        }, 1000)
      })
      innerAudioContext.onEnded((res) => {  console.log('播放结束!'); })
      // 播放音频失败的回调
      innerAudioContext.onError((res) => { console.log('播放音频失败' + res); })         
      innerAudioContext.onStop((res) => { console.log('停止播放!');  })
 
 
    }
  },
)

- 希望提供的能力

音频mp3播放,可以获取时长

回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容