# AudioContext

> The `AudioContext` JS object is the entry point of the Web Audio API for processing and playing audio.

## What is AudioContext in the Web Audio API?

The object that creates and connects audio nodes to generate, analyze, mix and play sound with precise timing. Used for visualizers, games and playing streamed speech. Browsers start it suspended until the user interacts with the page.

- Topic: Media & Audio
- Type: object
- Browser support: Chrome 35+, Chrome Android 35+, Edge 12+, Firefox 25+, Firefox Android 25+, Safari 14.1+, Safari iOS 14.5+

## Example

Wrong:

```js
const ctx = new AudioContext();
button.addEventListener('click', () => {
  playTone(ctx);
});
```

Right:

```js
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.

## Common mistakes

- Creating the context on page load and getting silence because autoplay is blocked.

## Key points

- `resume()` is called inside a user gesture handler before playing audio.
- One context is reused instead of creating a new one for every sound.

## Interview questions

### Why does AudioContext say it was not allowed to start?

Autoplay rules block audio until the user interacts with the page. Create the context or call `resume()` inside a click or key handler.

### How do I change the volume with the Web Audio API?

Route the source through a `GainNode` from `ctx.createGain()` and set `gain.value`, for example 0.5 for half volume.

### Should I close an AudioContext?

Yes. When audio is no longer needed, call `close()` to release the system audio resources.

## Related cards

- [AudioWorklet](https://wonderium.dev/js/audio-worklet.md)
- [MediaStream](https://wonderium.dev/js/media-stream.md)
- [speechSynthesis](https://wonderium.dev/js/speech-synthesis.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/audio-context
- Last updated: 2026-11-25
