Skip to content

fix audio source scope to enable stopping playback #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions fetch/fetch-array-buffer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h1>Fetch arrayBuffer example</h1>
const play = document.querySelector(".play");
const stop = document.querySelector(".stop");
const errorDisplay = document.querySelector(".error");
let currentSource = null;

// use fetch to load an audio track, and
// decodeAudioData to decode it and stick it in a buffer.
Expand Down Expand Up @@ -53,7 +54,8 @@ <h1>Fetch arrayBuffer example</h1>
getData()
.then((source) => {
errorDisplay.innerHTML = "";
source.start(0);
currentSource = source;
currentSource.start(0);
play.disabled = true;
})
.catch((error) => {
Expand All @@ -64,8 +66,10 @@ <h1>Fetch arrayBuffer example</h1>
};

stop.onclick = () => {
source.stop(0);
play.disabled = false;
if(currentSource){
currentSource.stop(0);
play.disabled = false;
}
};

// dump script to pre element
Expand Down