const button = document.querySelector("button");
button.addEventListener("click", () => {
const audioCtx = new AudioContext();
// Do something with the audio context
});Web Audio API best practices / notebook
Web Audio API best practices
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const audioCtx = new AudioContext();
const button = document.querySelector("button");
button.addEventListener("click", () => {
// check if context is in suspended state (autoplay policy)
if (audioCtx.state === "suspended") {
audioCtx.resume();
}
});gainNode.gain.value = 0.5;gainNode.gain.setValueAtTime(1, audioCtx.currentTime + 2);