问题1:ios手机播放音频 无法获取总时长,安卓正常获取
console.log('handleInitAudio.src:' + src);
this.innerAudioContext.onPlay(() => {
console.log('开始播放')
this.innerAudioContext.duration;
setTimeout(() => {
const durationStr = this.parseTime(this.innerAudioContext.duration)
console.log('onPlay播放总时长durationStr:' + durationStr);
this.setData({ durationStr: durationStr })
console.log('onPlay播放总时长data.durationStr:' + this.data.durationStr);
}, 1000)
})
let that =this;
this.innerAudioContext.onCanplay(() => {
// 这是一个迷,据说要手动先触发这个属性,后面才能用setTimeout获取真实时长
let duration = that.innerAudioContext.duration;
console.log(that.innerAudioContext);
console.log('初始化播放总时长innerAudioContext.duration:' + duration);
setTimeout(() => {
const durationStr = that.parseTime(that.innerAudioContext.duration)
console.log('播放总时长durationStr:' + durationStr);
that.setData({ durationStr: durationStr })
console.log('播放总时长data.durationStr:' + that.data.durationStr);
}, 1000)
})
问题2 :部分安卓手机无法播放音频
是有BUG,可以试下
const audioContext = wx.createInnerAudioContext(); const loadDuration = function () { setTimeout(() => { if (audioContext.duration === 0) { loadDuration(); } else { // 获取到正确的duration } }, 100); }; audioContext.onCanplay(loadDuration);
_this.data._laudioAudioContext.onCanplay(() => { if (_this.data.intervalLoadDuration) { return; } // 安卓要先play,再pause。。。。 _this.data._laudioAudioContext.volume = 0; _this.data._laudioAudioContext.play(); _this.data._laudioAudioContext.pause(); _this.data._laudioAudioContext.volume = 1; // 一般第一次获取不到。。。 _this.data.intervalLoadDuration = setInterval(() => { if (_this.data._laudioAudioContext.duration) { clearInterval(_this.data.intervalLoadDuration); console.log(_this.data._laudioAudioContext.duration) } }, 100); });
试试创建一个临时的innerAudioContext
然后播放这个音频文件
然后就能在onTimeUpdate里拿到时长了
最后记得停止播放录音并且销毁这个临时的innerAudioContext
试下放在onCanPlay的回调里读取duration