Loading...
const ctx = new AudioContext();
button.addEventListener('click', () => {
playTone(ctx);
});const ctx = new AudioContext();
button.addEventListener('click', async () => {
if (ctx.state === 'suspended') await ctx.resume();
playTone(ctx);
});A context created before any user interaction starts 'suspended', so resume it inside the gesture handler.
resume() is called inside a user gesture handler before playing audio.Autoplay rules block audio until the user interacts with the page. Create the context or call resume() inside a click or key handler.
Route the source through a GainNode from ctx.createGain() and set gain.value, for example 0.5 for half volume.
Yes. When audio is no longer needed, call close() to release the system audio resources.