feat(audio): 实现音乐自动播放控制逻辑
- 添加暂停时间记录与清除逻辑 - 实现24小时内暂停后不自动播放的判断 - 更新音频播放前的自动播放条件检查 - 优化本地存储中时间数据的读取与写入方式
This commit is contained in:
16
js/about.js
16
js/about.js
@@ -639,8 +639,12 @@ class UIManager {
|
|||||||
if (this.audio) {
|
if (this.audio) {
|
||||||
if (this.audio.paused) {
|
if (this.audio.paused) {
|
||||||
this.audio.play().catch(() => {});
|
this.audio.play().catch(() => {});
|
||||||
|
// 清除暂停时间记录,允许下次自动播放
|
||||||
|
localStorage.removeItem('musicPauseTime');
|
||||||
} else {
|
} else {
|
||||||
this.audio.pause();
|
this.audio.pause();
|
||||||
|
// 记录暂停时间
|
||||||
|
localStorage.setItem('musicPauseTime', new Date().getTime().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateLabels();
|
updateLabels();
|
||||||
@@ -653,7 +657,17 @@ class UIManager {
|
|||||||
if (!el) return;
|
if (!el) return;
|
||||||
this.audio = el;
|
this.audio = el;
|
||||||
this.audio.loop = true;
|
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();
|
tryPlay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user