Skip to content

Commit

Permalink
Revert "refactor: simplify audio playback"
Browse files Browse the repository at this point in the history
This reverts commit a43a634.
  • Loading branch information
dewanakl committed Mar 5, 2025
1 parent a43a634 commit d5201d6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion js/app/guest/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export const audio = (() => {
*/
let audioEl = null;

/**
* @type {Promise<void>|null}
*/
let canPlay = null;

let isPlay = false;

const statePlay = '<i class="fa-solid fa-circle-pause spin-button"></i>';
Expand All @@ -25,6 +30,8 @@ export const audio = (() => {
return;
}

await canPlay;

music.disabled = true;
try {
await audioEl.play();
Expand Down Expand Up @@ -62,6 +69,8 @@ export const audio = (() => {
audioEl.autoplay = false;
audioEl.controls = false;

canPlay = new Promise((res) => audioEl.addEventListener('canplay', res, { once: true }));

const context = new AudioContext();
const source = context.createMediaElementSource(audioEl);
source.connect(context.destination);
Expand All @@ -77,8 +86,8 @@ export const audio = (() => {
return;
}

sourceBuffer.addEventListener('updateend', push, { once: true });
sourceBuffer.appendBuffer(value.buffer);
sourceBuffer.addEventListener('updateend', push, { once: true });
});

push();
Expand Down

0 comments on commit d5201d6

Please sign in to comment.