diff --git a/index.html b/index.html index 4b4b5f0..a9bcbf5 100644 --- a/index.html +++ b/index.html @@ -1252,29 +1252,30 @@ function playTrack(i) { audioNext.src = t.url; audioNext.load(); initAudioCtx(); - const onReady = () => { - audioNext.removeEventListener('canplaythrough', onReady); - audioNext.play().catch(() => {}); - const now = audioCtx.currentTime; - const cd = crossfadeDuration; - const fg = audio === audioElA ? gainA : gainB; - const ng = audio === audioElA ? gainB : gainA; - fg.gain.setValueAtTime(1, now); - fg.gain.linearRampToValueAtTime(0, now + cd); - ng.gain.setValueAtTime(0, now); - ng.gain.linearRampToValueAtTime(1, now + cd); - clearTimeout(crossfadeTimer); - crossfadeTimer = setTimeout(() => { - crossfadeTimer = null; - if (audio === audioElA) { audioElA.pause(); audioElA.removeAttribute('src'); audioElA.load(); } - else { audioElB.pause(); audioElB.removeAttribute('src'); audioElB.load(); } + const onReady = () => { + audioNext.removeEventListener('canplaythrough', onReady); + audioNext.play().catch(() => {}); + const now = audioCtx.currentTime; + const cd = crossfadeDuration; + const fg = audio === audioElA ? gainA : gainB; + const ng = audio === audioElA ? gainB : gainA; + fg.gain.setValueAtTime(1, now); + fg.gain.linearRampToValueAtTime(0, now + cd); + ng.gain.setValueAtTime(0, now); + ng.gain.linearRampToValueAtTime(1, now + cd); audio = audioNext; audioNext = audio === audioElA ? audioElB : audioElA; - isCrossfading = false; - }, cd * 1000 + 100); - updateUI(); - renderTracks(); - }; + clearTimeout(crossfadeTimer); + crossfadeTimer = setTimeout(() => { + crossfadeTimer = null; + audioNext.pause(); + audioNext.removeAttribute('src'); + audioNext.load(); + isCrossfading = false; + }, cd * 1000 + 100); + updateUI(); + renderTracks(); + }; audioNext.addEventListener('canplaythrough', onReady); } else { if (audio.src !== t.url) { @@ -1371,6 +1372,7 @@ playBtn.addEventListener('click', () => { audio.play().catch(() => {}); } else { audio.pause(); + if (isCrossfading) audioNext.pause(); } }); prevBtn.addEventListener('click', prevTrack);