diff --git a/src/renderer/hooks/MusicHook.ts b/src/renderer/hooks/MusicHook.ts index 863d454..3c6de6f 100644 --- a/src/renderer/hooks/MusicHook.ts +++ b/src/renderer/hooks/MusicHook.ts @@ -149,6 +149,30 @@ const setupAudioListeners = () => { } }); + const replayMusic = async () => { + try { + // 如果当前有音频实例,先停止并销毁 + if (sound.value) { + sound.value.stop(); + sound.value.unload(); + sound.value = null; + } + + // 重新播放当前歌曲 + if (store.state.playMusicUrl && playMusic.value) { + const newSound = await audioService.play(store.state.playMusicUrl, playMusic.value); + sound.value = newSound as Howl; + setupAudioListeners(); + } else { + console.error('No music URL or playMusic data available'); + store.commit('nextPlay'); + } + } catch (error) { + console.error('Error replaying song:', error); + store.commit('nextPlay'); + } + }; + // 监听结束 audioService.on('end', () => { clearInterval(); @@ -156,23 +180,13 @@ const setupAudioListeners = () => { if (store.state.playMode === 1) { // 单曲循环模式 if (sound.value) { - sound.value.seek(0); - try { - sound.value.play(); - } catch (error) { - console.error('Error replaying song:', error); - store.commit('nextPlay'); - } + replayMusic(); } } else if (store.state.playMode === 2) { // 随机播放模式 const { playList } = store.state; if (playList.length <= 1) { - try { - sound.value?.play(); - } catch (error) { - console.error('Error replaying song:', error); - } + replayMusic(); } else { let randomIndex; do { @@ -466,13 +480,6 @@ if (isElectron) { // 在组件挂载时设置监听器 onMounted(() => { - const clearIntervalFn = setupAudioListeners(); + setupAudioListeners(); useLyricProgress(); // 直接调用,不需要解构返回值 - - // 在组件卸载时清理 - onUnmounted(() => { - clearIntervalFn(); - audioService.stop(); - audioService.clearAllListeners(); - }); }); diff --git a/src/renderer/layout/components/PlayBar.vue b/src/renderer/layout/components/PlayBar.vue index 40c1f80..bb4a894 100644 --- a/src/renderer/layout/components/PlayBar.vue +++ b/src/renderer/layout/components/PlayBar.vue @@ -167,6 +167,7 @@ import { import type { SongResult } from '@/type/music'; import { getImgUrl, isElectron, isMobile, secondToMinute, setAnimationClass } from '@/utils'; import { showShortcutToast } from '@/utils/shortcutToast'; +import { audioService } from '@/services/audioService'; import MusicFull from './MusicFull.vue'; @@ -282,16 +283,19 @@ const MusicFullRef = ref(null); // 播放暂停按钮事件 const playMusicEvent = async () => { - if (play.value) { - if (sound.value) { - sound.value.pause(); + try { + if (play.value) { + audioService.pause(); + store.commit('setPlayMusic', false); + } else { + audioService.play(); + store.commit('setPlayMusic', true); } - store.commit('setPlayMusic', false); - } else { - if (sound.value) { - sound.value.play(); + } catch (error) { + console.log('error',error) + if (play.value) { + store.commit('nextPlay'); } - store.commit('setPlayMusic', true); } }; diff --git a/src/renderer/services/audioService.ts b/src/renderer/services/audioService.ts index 69a731b..dc4a7ed 100644 --- a/src/renderer/services/audioService.ts +++ b/src/renderer/services/audioService.ts @@ -121,7 +121,11 @@ class AudioService { } // 播放控制相关 - play(url: string, track: SongResult): Promise { + play(url?: string, track?: SongResult): Promise { + if (this.currentSound && !url && !track) { + this.currentSound.play(); + return Promise.resolve(this.currentSound as Howl); + } return new Promise((resolve, reject) => { let retryCount = 0; const maxRetries = 3; @@ -131,10 +135,10 @@ class AudioService { this.currentSound.unload(); } this.currentSound = null; - this.currentTrack = track; + this.currentTrack = track as SongResult; this.currentSound = new Howl({ - src: [url], + src: [url as string], html5: true, autoplay: true, volume: localStorage.getItem('volume') @@ -163,7 +167,7 @@ class AudioService { }); // 更新媒体会话元数据 - this.updateMediaSessionMetadata(track); + this.updateMediaSessionMetadata(track as SongResult); // 设置音频事件监听 this.currentSound.on('play', () => { @@ -230,6 +234,13 @@ class AudioService { } } + pause() { + if (this.currentSound) { + this.currentSound.pause(); + } + } + + clearAllListeners() { this.callbacks = {}; }