uni.showLoading({
title: '音频加载中...',
mask: true
});
console.log('初始化音频:1111', this.audioContext);
if (this.audioContext) {
try {
this.audioContext.pause();
this.audioContext.destroy()
this.audioContext = null
} catch (e) {
console.log('销毁音频上下文失败:', e);
//TODO handle the exception
}
}
// 创建音频上下文
this.audioContext = uni.createInnerAudioContext();
if (path) {
this.audioContext.src = path;
} else {
this.audioContext.src = 'https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav';//音频文件
}
this.audioContext.playbackRate = this.currentSpeed;
// 监听音频加载完成
this.audioContext.onCanplay((e) => {
uni.hideLoading();
console.log('音频加载完成:', e);
// 监听播放进度
this.audioContext.onTimeUpdate(() => {
if (this.audioContext && this.audioContext.duration) {
this.playProgress = this.audioContext.currentTime / this.audioContext.duration;
}
});
});
// 监听音频加载错误
this.audioContext.onError((err) => {
console.error('音频加载错误:', err);
uni.hideLoading();
});
// 监听播放结束
this.audioContext.onEnded(() => {
this.isPlaying = false;
// 播放结束不重置进度,点击重新播放时会自动从头开始
});