diff --git a/themes/Ying/assets/js/shortcodes.js b/themes/Ying/assets/js/shortcodes.js index 31bd26b8..14c4d877 100644 --- a/themes/Ying/assets/js/shortcodes.js +++ b/themes/Ying/assets/js/shortcodes.js @@ -93,14 +93,23 @@ var AudioManager = { } catch (e) {} }, + // 重置播放器到初始状态(刷新页面/非 PJAX 导航时调用) + reset: function() { + if (this.globalAudio) { + this.globalAudio.pause(); + this.globalAudio.currentTime = 0; + this.globalAudio.src = ''; + } + this.globalMusicSrc = null; + this.globalMusicCard = null; + sessionStorage.removeItem(this.STORAGE_KEY); + }, + // 恢复播放状态 - 在 PJAX 切换后调用 restorePlayingState: function() { var state = this.restoreFromSession(); if (!state || !state.src) { - // 当前页面没有保存的状态,暂停播放 - if (this.globalAudio && !this.globalAudio.paused) { - this.globalAudio.pause(); - } + // 当前页面没有保存的状态,保持当前播放状态(继续播放) return; } @@ -119,10 +128,7 @@ var AudioManager = { } if (!targetCard) { - // 当前页面没有这个音乐卡片,暂停播放 - if (this.globalAudio && !this.globalAudio.paused) { - this.globalAudio.pause(); - } + // 当前页面没有这个音乐卡片,保持当前播放状态(继续播放) return; } @@ -131,7 +137,7 @@ var AudioManager = { this.globalMusicCard = targetCard; this.globalAudio.src = state.src; - // 更新 UI + // 更新 UI(恢复原来的播放状态) this._updateUI(targetCard, state.isPlaying); // 等待元数据加载完成 @@ -144,7 +150,8 @@ var AudioManager = { self._onTimeUpdate(); - if (state.isPlaying) { + // 如果之前在播放,继续播放 + if (state.isPlaying && self.globalAudio.paused) { self.globalAudio.play().catch(function() {}); } }); @@ -154,7 +161,8 @@ var AudioManager = { this.globalAudio.currentTime = state.currentTime; } this._onTimeUpdate(); - if (state.isPlaying) { + // 如果之前在播放,继续播放 + if (state.isPlaying && this.globalAudio.paused) { this.globalAudio.play().catch(function() {}); } } @@ -751,17 +759,25 @@ function cleanupAllShortcodes() { // ============================================================ function initAllShortcodes() { - // 初始化 AudioManager(创建全局音频元素,恢复播放状态) + // 初始化 AudioManager(创建全局音频元素) AudioManager.init(); initLivePhotoShortcodes(); initVoiceBubbles(); initMusicCards(); - // 恢复 PJAX 切换前的播放状态 - setTimeout(function() { - AudioManager.restorePlayingState(); - }, 100); + // 检查是否是 PJAX 导航 + if (window.__isPjaxNavigation) { + // PJAX 导航:恢复播放状态(进度和播放状态) + setTimeout(function() { + AudioManager.restorePlayingState(); + }, 100); + // 重置标志 + window.__isPjaxNavigation = false; + } else { + // 非 PJAX 导航(刷新页面/跳转):重置播放器到初始状态 + AudioManager.reset(); + } } // DOMContentLoaded @@ -776,7 +792,10 @@ if (document.readyState === 'interactive' || document.readyState === 'complete') } // PJAX support - cleanup before and re-initialize after page transition -document.addEventListener('pjax:beforeSend', cleanupAllShortcodes); +document.addEventListener('pjax:beforeSend', function() { + window.__isPjaxNavigation = true; + cleanupAllShortcodes(); +}); document.addEventListener('pjax:complete', initAllShortcodes); document.addEventListener('pjax:success', initAllShortcodes);