getSongUrl

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

View File

@@ -1277,31 +1277,43 @@
}, [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;
// 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) const fetchAndPlay = async () => {
// Note: audioRef.current.src returns full absolute URL // Update URL when quality changes or song changes
const currentSrc = audio.src; const url = await api.getSongUrl(currentSong.id, currentSong.platform || currentSong.source, quality);
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 (!isMounted || !url) return;
if (currentSrc !== url) {
audio.src = url; // Check if song changed while fetching
if (wasPlaying) { if (currentSongRef.current?.id !== currentSong.id) return;
playAudioWithFallback(audio, { deferOnIOS });
// 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 { } 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,11 +1429,16 @@
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 (audio.src !== url) { if (!url) return;
audio.src = url; // Ensure we are still trying to play the same song
} if (currentSongRef.current?.id === nextSong.id) {
playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS }); if (audio.src !== url) {
audio.src = url;
}
playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS });
}
});
} }
}; };