From 99d71f05cfd37479e1b431bb47b896fed8c929a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=82=A6?= Date: Wed, 14 Jan 2026 17:56:46 +0800 Subject: [PATCH] getSongUrl --- index.html | 63 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/index.html b/index.html index 5d7e4ad..ab14adf 100644 --- a/index.html +++ b/index.html @@ -1277,31 +1277,43 @@ }, [playlist, currentSong, mode, volume, quality]); useEffect(() => { + let isMounted = true; if (currentSong) { const audio = audioRef.current; if (!audio) return; - // Update URL when quality changes or song changes - const url = api.getSongUrl(currentSong.id, currentSong.platform || currentSong.source, quality); - - // Only update src if it's different to avoid reloading same song on re-render (unless quality changed) - // Note: audioRef.current.src returns full absolute URL - const currentSrc = audio.src; - const wasPlaying = isPlaying; - const deferOnIOS = autoNextPendingRef.current; - autoNextPendingRef.current = false; - - // Simple check if src changed significantly (avoiding minor encoding diffs if possible, but exact match is safer) - if (currentSrc !== url) { - audio.src = url; - if (wasPlaying) { - playAudioWithFallback(audio, { deferOnIOS }); + + const fetchAndPlay = async () => { + // Update URL when quality changes or song changes + const url = await api.getSongUrl(currentSong.id, currentSong.platform || currentSong.source, quality); + + if (!isMounted || !url) return; + + // Check if song changed while fetching + if (currentSongRef.current?.id !== currentSong.id) return; + + // Only update src if it's different to avoid reloading same song on re-render (unless quality changed) + // Note: audioRef.current.src returns full absolute URL + const currentSrc = audio.src; + const wasPlaying = isPlaying; + const deferOnIOS = autoNextPendingRef.current; + autoNextPendingRef.current = false; + + // Simple check if src changed significantly (avoiding minor encoding diffs if possible, but exact match is safer) + if (currentSrc !== url) { + audio.src = url; + if (wasPlaying) { + playAudioWithFallback(audio, { deferOnIOS }); + } + } else if (deferOnIOS && wasPlaying) { + playAudioWithFallback(audio, { deferOnIOS: true }); } - } else if (deferOnIOS && wasPlaying) { - playAudioWithFallback(audio, { deferOnIOS: true }); - } + }; + + fetchAndPlay(); } else { autoNextPendingRef.current = false; } + return () => { isMounted = false; }; }, [currentSong, quality]); // Re-run when quality changes useEffect(() => { @@ -1417,11 +1429,16 @@ if (options.immediate) { const audio = audioRef.current; if (!audio || !nextSong) return; - const url = api.getSongUrl(nextSong.id, nextSong.platform || nextSong.source, quality); - if (audio.src !== url) { - audio.src = url; - } - playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS }); + api.getSongUrl(nextSong.id, nextSong.platform || nextSong.source, quality).then(url => { + if (!url) return; + // Ensure we are still trying to play the same song + if (currentSongRef.current?.id === nextSong.id) { + if (audio.src !== url) { + audio.src = url; + } + playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS }); + } + }); } };