|
|
@@ -17,6 +17,7 @@ const progressBar = document.getElementById("progressBar");
|
|
|
const audioCacheName = "musicweb-audio-cache-v1";
|
|
|
let toastTimer = null;
|
|
|
let featuredTimer = null;
|
|
|
+let playRequestId = 0;
|
|
|
|
|
|
const iconMarkup = {
|
|
|
play: '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M8 5v14l11-7-11-7z"/></svg>',
|
|
|
@@ -667,6 +668,7 @@ function syncPlayButton() {
|
|
|
|
|
|
async function getCachedAudioUrl(track) {
|
|
|
if (track.path?.startsWith("cloud/")) {
|
|
|
+ showIconToast("云端加载中");
|
|
|
const response = await fetch(`/api/cloud-url/${track.path}`);
|
|
|
if (!response.ok) throw new Error(`cloud url request failed: ${response.status}`);
|
|
|
const data = await response.json();
|
|
|
@@ -696,18 +698,29 @@ async function getCachedAudioUrl(track) {
|
|
|
|
|
|
async function playTrack(index) {
|
|
|
if (!state.currentQueue.length) return;
|
|
|
+ const requestId = ++playRequestId;
|
|
|
state.currentTrackIndex = index;
|
|
|
const track = state.currentQueue[index];
|
|
|
try {
|
|
|
- audio.src = await getCachedAudioUrl(track);
|
|
|
+ const resolvedUrl = await getCachedAudioUrl(track);
|
|
|
+ if (requestId !== playRequestId) return;
|
|
|
+ audio.pause();
|
|
|
+ audio.removeAttribute("src");
|
|
|
+ audio.load();
|
|
|
+ audio.src = resolvedUrl;
|
|
|
await audio.play();
|
|
|
} catch (error) {
|
|
|
+ if (requestId !== playRequestId) return;
|
|
|
console.error("playTrack failed", track?.path, error);
|
|
|
+ audio.pause();
|
|
|
+ audio.removeAttribute("src");
|
|
|
+ audio.load();
|
|
|
audio.src = track.url;
|
|
|
audio.play().catch((fallbackError) => {
|
|
|
console.error("fallback play failed", track?.path, fallbackError);
|
|
|
});
|
|
|
}
|
|
|
+ if (requestId !== playRequestId) return;
|
|
|
updateDock(track);
|
|
|
renderQueue();
|
|
|
savePlaybackState();
|