fix(audio store): FM 切换歌曲

feature/artists
mackt 7 months ago
parent 27305f6248
commit c2a469a0ab

@ -94,30 +94,39 @@ const useAudioState = create<AuioState>()(
// FM 模式,切换歌曲 // FM 模式,切换歌曲
const switchFMSong = async (diff: number) => { const switchFMSong = async (diff: number) => {
if (diff === 0) return; if (diff === 0) return;
const { playQueue, playList } = get(); let { playQueue, playList } = get();
if (!playQueue) return; if (!playQueue) return;
const index = playQueue.findIndex((item) => item.id === get().audioId); // 获取当前播放歌曲, 在 Queue 中的 index并计算出下一首歌曲的 index
const nextIndex = index + diff; // 计算后的 index const queueIndex = playQueue.findIndex((item) => item.id === get().audioId);
const listIndex = playList.findIndex((item) => item.id === get().audioId);
const nextQueueIndex = queueIndex + diff;
const nextListIndex = listIndex + diff;
// 播放 list 最后一首歌时再加载30首 // 播放 list 最后一首歌时再加载30首
if (nextIndex >= playList.length - 1) { if (nextListIndex >= playList.length - 1) {
await getFm(); await getFm();
playQueue = get().playQueue;
playList = get().playList;
} }
// 判断 index 是否越界,计算音频 id // 切换歌曲
let audioId = ''; let audioId = '';
if (nextIndex < 0) { if (nextQueueIndex < 0) {
// 在第一首歌点击上一首,切换到最后一首
audioId = playQueue[playQueue.length - 1].id; audioId = playQueue[playQueue.length - 1].id;
} else if (nextIndex >= playQueue.length) { } else if (nextQueueIndex >= playQueue.length) {
const newQueue = [...playQueue, playList[nextIndex]]; // 在最后一首歌点击下一首,往 Queue 中 push 歌曲
const newAudio = playList[nextListIndex];
set( set(
produce((state) => { produce((state) => {
state.playQueue = newQueue; state.playQueue = [...playQueue, newAudio];
}), }),
); );
audioId = newQueue[nextIndex].id; audioId = newAudio.id;
} else { } else {
audioId = playQueue[nextIndex].id; // 非越界切换
audioId = playQueue[nextQueueIndex].id;
} }
// 切换歌曲 // 切换歌曲

Loading…
Cancel
Save