From 6ffe4daed0d4b094a57a91b3466f491a0894cdb1 Mon Sep 17 00:00:00 2001 From: algerkong Date: Fri, 2 May 2025 19:54:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E5=88=97=E8=A1=A8=E7=B4=A2=E5=BC=95=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91=EF=BC=8C=E9=81=BF=E5=85=8D=E4=B8=8E?= =?UTF-8?q?=20nextPlay/prevPlay=20=E5=86=B2=E7=AA=81=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E6=AD=8C=E6=9B=B2=E9=A2=84=E5=8A=A0=E8=BD=BD=E4=B8=80?= =?UTF-8?q?=E8=87=B4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/store/modules/player.ts | 31 +++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/renderer/store/modules/player.ts b/src/renderer/store/modules/player.ts index 3523d9b..71046a1 100644 --- a/src/renderer/store/modules/player.ts +++ b/src/renderer/store/modules/player.ts @@ -347,14 +347,21 @@ export const usePlayerStore = defineStore('player', () => { musicHistory.addMusic(playMusic.value); - const newIndex = playList.value.findIndex( + // 找到歌曲在播放列表中的索引,如果是通过 nextPlay/prevPlay 调用的,不会更新 playListIndex + const songIndex = playList.value.findIndex( (item: SongResult) => item.id === music.id && item.source === music.source ); - // 只有在找到歌曲时才更新索引 - if (newIndex !== -1) { - playListIndex.value = newIndex; - fetchSongs(playList.value, playListIndex.value + 1, playListIndex.value + 3); + // 只有在 songIndex 有效,并且与当前 playListIndex 不同时才更新 + // 这样可以避免与 nextPlay/prevPlay 中的索引更新冲突 + if (songIndex !== -1 && songIndex !== playListIndex.value) { + console.log('歌曲索引不匹配,更新为:', songIndex); + playListIndex.value = songIndex; + } + + // 无论如何都预加载更多歌曲 + if (songIndex !== -1) { + fetchSongs(playList.value, songIndex + 1, songIndex + 3); } else { console.warn('当前歌曲未在播放列表中找到'); } @@ -390,10 +397,14 @@ export const usePlayerStore = defineStore('player', () => { const setPlay = async (song: SongResult) => { try { - await handlePlayMusic(song); + // 直接调用 handlePlayMusic,它会处理索引更新和播放逻辑 + const success = await handlePlayMusic(song); + + // 记录到本地存储,保持一致性 localStorage.setItem('currentPlayMusic', JSON.stringify(playMusic.value)); localStorage.setItem('currentPlayMusicUrl', playMusicUrl.value); - return true; + + return success; } catch (error) { console.error('设置播放失败:', error); return false; @@ -470,6 +481,9 @@ export const usePlayerStore = defineStore('player', () => { nowPlayListIndex = (playListIndex.value + 1) % playList.value.length; } + // 重要:首先更新当前播放索引 + playListIndex.value = nowPlayListIndex; + // 获取下一首歌曲 const nextSong = playList.value[nowPlayListIndex]; @@ -526,6 +540,9 @@ export const usePlayerStore = defineStore('player', () => { const nowPlayListIndex = (playListIndex.value - 1 + playList.value.length) % playList.value.length; + // 重要:首先更新当前播放索引 + playListIndex.value = nowPlayListIndex; + // 获取上一首歌曲 const prevSong = playList.value[nowPlayListIndex];