mirror of
https://codeberg.org/armin/noiz.git
synced 2026-09-01 05:50:52 +02:00
fix: swap audio element immediately during crossfade so pause controls the correct track
This commit is contained in:
parent
fddbdf9a1c
commit
ba7d3d0310
1 changed files with 23 additions and 21 deletions
44
index.html
44
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue