feat(audio): 实现音乐自动播放控制逻辑

- 添加暂停时间记录与清除逻辑
- 实现24小时内暂停后不自动播放的判断
- 更新音频播放前的自动播放条件检查
- 优化本地存储中时间数据的读取与写入方式
This commit is contained in:
hehh
2025-11-23 21:54:53 +08:00
parent 557666403d
commit 121972898d

View File

@@ -639,8 +639,12 @@ class UIManager {
if (this.audio) {
if (this.audio.paused) {
this.audio.play().catch(() => {});
// 清除暂停时间记录,允许下次自动播放
localStorage.removeItem('musicPauseTime');
} else {
this.audio.pause();
// 记录暂停时间
localStorage.setItem('musicPauseTime', new Date().getTime().toString());
}
}
updateLabels();
@@ -653,7 +657,17 @@ class UIManager {
if (!el) return;
this.audio = el;
this.audio.loop = true;
const tryPlay = () => { this.audio.play().catch(() => {}); };
// 检查是否在24小时内用户暂停过音乐
const pauseTime = localStorage.getItem('musicPauseTime');
const now = new Date().getTime();
const shouldAutoPlay = !(pauseTime && (now - parseInt(pauseTime)) < 24 * 60 * 60 * 1000);
const tryPlay = () => {
if (shouldAutoPlay) {
this.audio.play().catch(() => {});
}
};
tryPlay();
}