相关代码
// 加载音频处理
handleLoadAudio() {
// 获取当前swiper下的audio
const audio = this.data.quesList[this.data.active].recordPath;
this.data.innerAudioContext.src = encodeURI(audio);
let that = this;
this.data.innerAudioContext.onPlay(() => {
this.setData({
progressEndVal: Math.floor(this.data.innerAudioContext.duration),
});
console.log("开始播放");
});
// 监听onCanplay方法,获取音频时长
this.data.innerAudioContext.onCanplay(() => {
that.data.innerAudioContext.duration;
setTimeout(() => {
// 音频时长
this.setData({
audioDuration: that.data.innerAudioContext.duration,
countDownAudioTime: this.formarAudioTime(
Math.floor(that.data.innerAudioContext.duration)
),
progressStartVal: 0,
progressVal: 0,
});
console.log("duration", that.data.innerAudioContext.duration);
}, 500);
});
// 监听onTimeUpdate 方法,更新播放时间
// this.data.innerAudioContext.onTimeUpdate(() => {
// console.log("更新播放");
// that.setData({
// progressEndVal: that.data.innerAudioContext.duration.toFixed(0),
// progressVal: that.data.innerAudioContext.currentTime.toFixed(0),
// });
// });
// 播放结束
this.data.innerAudioContext.onEnded(() => {
this.setData({
playbackRate: "",
// playStatus: "ready",
});
console.log("end");
});
// 播放异常
this.data.innerAudioContext.onError((err) => {
this.setData({
playbackRate: "",
// playStatus: "ready",
});
this.handledoPlayAudio();
log.error(err);
});
this.data.innerAudioContext.onWaiting((err) => {
console.log("onWaiting", err);
});
},
// 正常倍速播放音频
handledoPlayAudio() {
this.data.innerAudioContext.playbackRate = 1;
this.changePlayAudio(1);
},
// 低倍速播放音频
handlePlaybackRate() {
this.data.innerAudioContext.playbackRate = 0.8;
this.changePlayAudio(0.8);
},
/**
* 控制播放音频
* @param {*} playbackRate 倍速
*/
changePlayAudio(playbackRate) {
this.setData({
playbackRate,
});
this.audioCountDown(this.data.audioDuration, playbackRate);
// this.resetSlider();
// this.sliderCountDown(
// 0,
// playbackRate,
// Math.floor(this.data.innerAudioContext.duration)
// );
this.getPlayStatus();
},
// 监听播放状态
getPlayStatus() {
// if (this.data.playStatus === "playing") {
// this.data.innerAudioContext.stop();
// setTimeout(() => {
// this.data.innerAudioContext.play();
// wx.hideLoading();
// }, 200);
// this.setData({
// playStatus: "ready",
// });
// } else {
// this.data.innerAudioContext.play();
// }
this.data.innerAudioContext.stop();
this.data.innerAudioContext.play();
},
/**
* 音频播放倒计时
* @param {*} seconds 音频时长
* @param {*} playbackRate 倍速
*/
audioCountDown(seconds, playbackRate) {
if (interval) {
clearInterval(interval);
}
interval = setInterval(
() => {
// 转换为整数
seconds = Number.parseInt(seconds);
// 获取分秒
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
// 格式化时间
const time = [
minutes.toString().padStart(2, "0"),
secs.toString().padStart(2, "0"),
].join(":");
// 输出或显示时间
this.setData({
countDownAudioTime: time,
});
// 如果秒数为0,清除interval并退出
if (seconds === 0) {
clearInterval(interval);
}
// 递减秒数
seconds -= 1;
},
playbackRate === 1 ? 1000 : 1250
);
},
[19:20:13] {"errMsg":"operateAudio:fail jsapi has no permission, event=operateAudio, runningState=background, permissionMsg=permission got, detail=jsapi has no permission, appId=xxxx","errCode":-1}
[19:13:03] {"errMsg":"operateAudio:fail:access denied, appId=xxxx","errCode":-1}
[19:15:03] {"errMsg":"operateAudio:fail audioInstance is not set","errCode":-1}