[基础库2.19.5] Audio动态设置src后,audi实例监听到fail: undefined
发布于 4 年前 作者 ufan 11866 次浏览 来自 问答
错误内容 test_audio 会监听到onError事件
 {"errCode":{"type":"error","errMsg":"set audio src http://wx.kemu3.cn/kemu3/audios/2021_07_03_5ffc1ef67a844715a3d2aacf06a0a5df.mp3 fail: undefined"}

代码内容
动态更新test_audio的src
this.test_audio.src = util.audioHost + '/kemu3/audios/' + audioPath;

// 监听test_audio的音频事件
export function test_audio_events() {

  // 监听音频进入可以播放状态的事件。但不保证后面可以流畅播放

  this.test_audio.onCanplay((e) => {

    console.log(e);

    console.log("监听音频进入可以播放状态的事件。但不保证后面可以流畅播放 onCanplay");

  })

  // 监听音频播放事件

  this.test_audio.onPlay((e) => {

    console.log("监听音频播放事件 onPlay");

  })

  // 监听音频暂停事件

  this.test_audio.onPause((e) => {

    console.log("监听音频暂停事件 onPause");

  })

  // 监听音频停止事件

  this.test_audio.onStop((e) => {

    console.log("监听音频停止事件 onStop");

  })

  // 监听音频自然播放至结束的事件

  // this.test_audio.onEnded((e) => {

  //   console.log(e);

  //   console.log("监听音频自然播放至结束的事件 onEnded");

  // })

  // 监听音频播放错误事件

  this.test_audio.onError((e) => {

    if (this.current_test === 0) return false;

    const { audioPath, examRoom, question } = this.current_test;

    const content = { ...e, page: util.getCurrentPageUrl(), audioPath, examRoom, question };

    console.log("监听音频播放错误事件 onError", content);

    log.error("音频播放错误:" + JSON.stringify(content));

    log.setFilterMsg('testAudioError');

    wx.showModal({

      title: '提示',

      content: `“${content.question}”音频播放失败,请进入小程序重试!`,

      showCancel: false,

      confirmText: '知道了',

      success: function (res) {

        if (res.confirm) {

          try {

            // console.log(content.page);

            // wx.clearStorageSync();

            wx.reLaunch({

              url: '/pages/index/index'

              // url: '/' + content.page

            });

          } catch(e) {

            // Do something when catch error

          }

        } else if (res.cancel) {

          console.log('用户点击取消')

        }

      }

    })

  })

  // 监听音频加载中事件。当音频因为数据不足,需要停下来加载时会触发

  this.test_audio.onWaiting((e) => {

    console.log("监听音频加载中事件。当音频因为数据不足,需要停下来加载时会触发 onWaiting");

  })

}
1 回复

加延时吧老铁

回到顶部