diff --git a/src/renderer/App.vue b/src/renderer/App.vue index 348a616..9a1306c 100644 --- a/src/renderer/App.vue +++ b/src/renderer/App.vue @@ -12,7 +12,7 @@ diff --git a/src/renderer/hooks/MusicHook.ts b/src/renderer/hooks/MusicHook.ts index 132bc88..dd618fb 100644 --- a/src/renderer/hooks/MusicHook.ts +++ b/src/renderer/hooks/MusicHook.ts @@ -1,5 +1,5 @@ import { createDiscreteApi } from 'naive-ui'; -import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'; +import { computed, nextTick, onUnmounted, ref, watch } from 'vue'; import i18n from '@/../i18n/renderer'; import useIndexedDB from '@/hooks/IndexDBHook'; @@ -763,37 +763,74 @@ if (isElectron) { } // 在组件挂载时设置监听器 -onMounted(() => { - // 初始化音频监听器 - setupAudioListeners(); - - // 监听歌词窗口关闭事件 - if (isElectron) { - window.api.onLyricWindowClosed(() => { - isLyricWindowOpen.value = false; - }); - } - - // 检查是否需要初始化 sound 对象 - if (!sound.value && audioService.getCurrentSound()) { - sound.value = audioService.getCurrentSound(); - - // 如果当前处于播放状态,启动进度更新 - if (playerStore.play && sound.value) { - // 如果有保存的播放进度,应用它 - if (playerStore.savedPlayProgress !== undefined && sound.value) { - try { - // 设置音频位置 - sound.value.seek(playerStore.savedPlayProgress); - // 同时更新时间显示,这样进度条也会更新 - nowTime.value = playerStore.savedPlayProgress; - console.log('恢复播放进度:', playerStore.savedPlayProgress); - } catch (e) { - console.error('恢复播放进度失败:', e); - } - } - - startProgressAnimation(); +export const initAudioListeners = async () => { + try { + // 确保有正在播放的音乐 + if (!playerStore.playMusic || !playerStore.playMusic.id) { + console.log('没有正在播放的音乐,跳过音频监听器初始化'); + return; } + + // 确保有音频实例 + const initialSound = audioService.getCurrentSound(); + if (!initialSound) { + console.log('没有音频实例,等待音频加载...'); + // 等待音频加载完成 + await new Promise((resolve) => { + const checkInterval = setInterval(() => { + const sound = audioService.getCurrentSound(); + if (sound) { + clearInterval(checkInterval); + resolve(); + } + }, 100); + + // 设置超时 + setTimeout(() => { + clearInterval(checkInterval); + console.log('等待音频加载超时'); + resolve(); + }, 5000); + }); + } + + // 初始化音频监听器 + setupAudioListeners(); + + // 监听歌词窗口关闭事件 + if (isElectron) { + window.api.onLyricWindowClosed(() => { + isLyricWindowOpen.value = false; + }); + } + + // 获取最新的音频实例 + const finalSound = audioService.getCurrentSound(); + if (finalSound) { + // 更新全局 sound 引用 + sound.value = finalSound; + + // 如果当前处于播放状态,启动进度更新 + if (playerStore.play) { + // 如果有保存的播放进度,应用它 + if (playerStore.savedPlayProgress !== undefined) { + try { + // 设置音频位置 + finalSound.seek(playerStore.savedPlayProgress); + // 同时更新时间显示 + nowTime.value = playerStore.savedPlayProgress; + console.log('恢复播放进度:', playerStore.savedPlayProgress); + } catch (e) { + console.error('恢复播放进度失败:', e); + } + } + + startProgressAnimation(); + } + } else { + console.warn('无法获取音频实例,跳过进度更新初始化'); + } + } catch (error) { + console.error('初始化音频监听器失败:', error); } -}); +}; diff --git a/src/renderer/store/modules/player.ts b/src/renderer/store/modules/player.ts index b0bebec..7c33d80 100644 --- a/src/renderer/store/modules/player.ts +++ b/src/renderer/store/modules/player.ts @@ -359,7 +359,7 @@ export const usePlayerStore = defineStore('player', () => { try { console.log('settingStore.setData', settingStore.setData); const isPlaying = settingStore.setData.autoPlay; - await handlePlayMusic(savedPlayMusic, isPlaying); + await handlePlayMusic({ ...savedPlayMusic, playMusicUrl: undefined }, isPlaying); if (savedProgress) { try { diff --git a/src/renderer/store/modules/settings.ts b/src/renderer/store/modules/settings.ts index 782113a..00133ac 100644 --- a/src/renderer/store/modules/settings.ts +++ b/src/renderer/store/modules/settings.ts @@ -37,6 +37,7 @@ export const useSettingsStore = defineStore('settings', () => { if (isElectron) { window.electron.ipcRenderer.send('set-store-value', 'set', cloneDeep(mergedData)); + console.log('mergedData', mergedData); setData.value = cloneDeep(mergedData); } else { localStorage.setItem('appSettings', JSON.stringify(cloneDeep(mergedData))); diff --git a/src/renderer/views/set/index.vue b/src/renderer/views/set/index.vue index ccee748..7461bea 100644 --- a/src/renderer/views/set/index.vue +++ b/src/renderer/views/set/index.vue @@ -461,9 +461,11 @@