This commit is contained in:
lcw
2026-04-15 16:04:50 +08:00
parent 763057ed9f
commit fbf259663b
41 changed files with 3651 additions and 2148 deletions

View File

@ -23,29 +23,18 @@ class AudioPlayerClass {
this.audioPlayer = new Audio(audioPath);
this.audioPlayer.loop = loop; // 设置循环播放
// 监听音频加载完成事件
this.audioPlayer.addEventListener('canplaythrough', () => {
console.log('音频加载完成,可以播放');
this.isLoaded = true;
resolve(true);
});
// 监听错误事件
this.audioPlayer.addEventListener('error', (error) => {
console.error('音频加载错误:', error);
console.error('错误代码:', error.code);
console.error('音频网络状态:', this.audioPlayer.networkState);
console.error('音频就绪状态:', this.audioPlayer.readyState);
reject(error);
});
// 预加载音频
console.log('开始预加载音频...');
this.audioPlayer.load();
console.log('音频预加载请求已发送');
} catch (error) {
console.error('初始化音频播放器失败:', error);
console.error('错误详情:', error.stack);
reject(error);
}
});
@ -79,26 +68,19 @@ class AudioPlayerClass {
this.audioPlayer.play()
.then(() => {
this.isPlaying = true;
console.log('音频播放成功');
resolve(true);
})
.catch(error => {
if (error.name === 'NotAllowedError') {
console.error('播放被浏览器自动播放策略阻止,需要用户交互后才能播放');
}
reject(error);
});
.catch(error => reject(error));
}, 0);
} else {
this.audioPlayer.play()
.then(() => {
this.isPlaying = true;
console.log('音频播放成功');
resolve(true);
})
.catch(error => {
if (error.name === 'NotAllowedError') {
console.error('播放被浏览器自动播放策略阻止,需要用户交互后才能播放');
// 浏览器自动播放策略阻止,静默忽略
}
reject(error);
});
@ -117,7 +99,6 @@ class AudioPlayerClass {
if (this.audioPlayer) {
this.audioPlayer.pause();
this.isPlaying = false;
console.log('音频已暂停');
}
}
@ -167,7 +148,6 @@ class AudioPlayerClass {
this.audioPlayer = null;
this.isLoaded = false;
this.isPlaying = false;
console.log('音频播放器已销毁');
}
}
}