feat(audio): 改进音频自动播放逻辑
- 在HTML中将音频preload属性从none改为auto - 添加用户交互检测以提高自动播放成功率 - 使用setTimeout延迟播放尝试以绕过浏览器限制 - 移除不必要的console.error输出 - 优化CSS以确保评论头像正确显示
This commit is contained in:
20
js/about.js
20
js/about.js
@@ -1133,22 +1133,22 @@ class UIManager {
|
||||
const shouldRemainPaused = this.shouldMusicRemainPaused();
|
||||
// 如果不应该保持暂停状态,则尝试播放
|
||||
if (!shouldRemainPaused) {
|
||||
this.audio.autoplay = true;
|
||||
let userInteracted = true;
|
||||
this.audio.play().catch(() => {
|
||||
// 静默处理播放失败
|
||||
userInteracted = false;
|
||||
});
|
||||
// 添加用户交互检查,避免浏览器阻止自动播放
|
||||
const attemptAutoplay = () => {
|
||||
// 检查是否已有用户交互
|
||||
if (this.userInteracted) {
|
||||
this.audio.play().catch(() => {
|
||||
// 静默处理播放失败
|
||||
console.error('Failed to play audio.');
|
||||
});
|
||||
} else {
|
||||
if (this.userInteracted === false) {
|
||||
// 添加一次性用户交互监听器
|
||||
const enableAudio = () => {
|
||||
this.userInteracted = true;
|
||||
this.audio.play().catch(() => {
|
||||
console.error('Failed to play audio.');
|
||||
});
|
||||
setTimeout(() => {
|
||||
this.audio.play().catch(() => {
|
||||
});
|
||||
}, 1000);
|
||||
document.removeEventListener('click', enableAudio);
|
||||
document.removeEventListener('touchstart', enableAudio);
|
||||
document.removeEventListener('keydown', enableAudio);
|
||||
|
||||
Reference in New Issue
Block a user