getSongUrl

This commit is contained in:
史悦
2026-01-14 17:56:46 +08:00
parent 87aa994365
commit 99d71f05cf

View File

@@ -1277,11 +1277,19 @@
}, [playlist, currentSong, mode, volume, quality]); }, [playlist, currentSong, mode, volume, quality]);
useEffect(() => { useEffect(() => {
let isMounted = true;
if (currentSong) { if (currentSong) {
const audio = audioRef.current; const audio = audioRef.current;
if (!audio) return; if (!audio) return;
const fetchAndPlay = async () => {
// Update URL when quality changes or song changes // Update URL when quality changes or song changes
const url = api.getSongUrl(currentSong.id, currentSong.platform || currentSong.source, quality); 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) // 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 // Note: audioRef.current.src returns full absolute URL
@@ -1299,9 +1307,13 @@
} else if (deferOnIOS && wasPlaying) { } else if (deferOnIOS && wasPlaying) {
playAudioWithFallback(audio, { deferOnIOS: true }); playAudioWithFallback(audio, { deferOnIOS: true });
} }
};
fetchAndPlay();
} else { } else {
autoNextPendingRef.current = false; autoNextPendingRef.current = false;
} }
return () => { isMounted = false; };
}, [currentSong, quality]); // Re-run when quality changes }, [currentSong, quality]); // Re-run when quality changes
useEffect(() => { useEffect(() => {
@@ -1417,12 +1429,17 @@
if (options.immediate) { if (options.immediate) {
const audio = audioRef.current; const audio = audioRef.current;
if (!audio || !nextSong) return; if (!audio || !nextSong) return;
const url = api.getSongUrl(nextSong.id, nextSong.platform || nextSong.source, quality); 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) { if (audio.src !== url) {
audio.src = url; audio.src = url;
} }
playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS }); playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS });
} }
});
}
}; };
const playPrev = () => { const playPrev = () => {