Web Audio API in JavaScript
Play a short tone in JavaScript with AudioContext, an oscillator, and a gain node. A click resumes audio; the quiet 440 Hz tone stops after 0.3 seconds.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<button id="beep">Play short tone</button><p id="status"></p>7<script>8let context;9document.querySelector('#beep').onclick = async () => {10 const status = document.querySelector('#status');11 const Audio = window.AudioContext || window.webkitAudioContext;12 if (!Audio) { status.textContent = 'Audio API not supported.'; return; }13 try {14 context ??= new Audio();15 await context.resume();16 const tone = context.createOscillator();17 const volume = context.createGain();18 tone.frequency.value = 440;19 volume.gain.value = 0.05;20 tone.connect(volume).connect(context.destination);21 tone.start();22 tone.stop(context.currentTime + 0.3);23 tone.onended = () => { tone.disconnect(); volume.disconnect(); };24 status.textContent = 'Playing a short 440 Hz tone.';25 } catch { status.textContent = 'Audio could not start.'; }26};27</script>28</body>29</html>
Sample Output
Playing a short 440 Hz tone.
The page scheduled a quiet 0.3-second tone. Muting or output-device settings can still prevent hearing it.
In this one
- Start sound after a click
- Connect a source through a volume node
- Stop a tone on the audio clock
- Reuse an audio context
How it works
- 1
Find a supported AudioContext constructor.
- 2
Create the context once and resume it inside the click handler.
- 3
Create an oscillator at 440 Hz and a quiet gain node.
- 4
Connect the nodes and schedule a stop after 0.3 seconds.
- 5
Read the status and listen for the short tone; muted devices may be silent.
Edge cases
Autoplay restriction
Sound may not start automatically.
Keep context creation or resume in a real click handler. Handle failure instead of assuming every browser will play it.
No sound heard
The page can run while the device is muted.
Check output volume and the selected audio device. A status message is not proof that the speaker produced sound.
What's going on
The Web Audio API creates and connects sound sources in the browser. An API is a set of features JavaScript can call. This small example makes a short practice tone rather than starting a sound that continues forever.
Click the button in the saved HTML page. An AudioContext manages the sound work, an oscillator produces a repeating wave, and a gain node controls volume. A node is one part of the audio path. Connecting the oscillator through the gain node to the destination sends a quiet tone to the speakers.
The frequency is 440 Hz, which means 440 wave cycles each second. The stop time uses the audio context's own clock, not a page timer. The click resumes a context that the browser may have paused under autoplay rules. The code disconnects the used nodes after the tone ends. Start with a low volume and let people choose whether to play sound.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which node sets volume?
Pick an answer to check it.
Question 2
Which clock schedules the stop?
Pick an answer to check it.
Questions people ask
Why not start this when the page loads?
Browsers often block unsolicited audio. A button also lets the person choose when to hear it.
What controls loudness here?
The gain node has a value of 0.05. That keeps the example quiet compared with connecting the oscillator directly.
Can I start the same oscillator again?
No. Make a new oscillator for each tone, as the click handler does.