音频mp3播放,开发工具没问题,真机模拟无法获取duration,无法播放
- 需求的场景描述(希望解决的问题) 音频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播放,可以获取时长