W3cubDocs

/Web APIs

AudioContext

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode.

An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything happens inside a context. It's recommended to create one AudioContext and reuse it instead of initializing a new one each time, and it's OK to use a single AudioContext for several different audio sources and pipeline concurrently.

EventTarget BaseAudioContext AudioContext

Constructor

AudioContext()

Creates and returns a new AudioContext object.

Instance properties

Also inherits properties from its parent interface, BaseAudioContext.

AudioContext.baseLatency Read only

Returns the number of seconds of processing latency incurred by the AudioContext passing the audio from the AudioDestinationNode to the audio subsystem.

AudioContext.outputLatency Read only

Returns an estimation of the output latency of the current audio context.

AudioContext.sinkId Read only Experimental

Returns the sink ID of the current output audio device.

Instance methods

Also inherits methods from its parent interface, BaseAudioContext.

AudioContext.close()

Closes the audio context, releasing any system audio resources that it uses.

AudioContext.createMediaElementSource()

Creates a MediaElementAudioSourceNode associated with an HTMLMediaElement. This can be used to play and manipulate audio from <video> or <audio> elements.

AudioContext.createMediaStreamSource()

Creates a MediaStreamAudioSourceNode associated with a MediaStream representing an audio stream which may come from the local computer microphone or other sources.

AudioContext.createMediaStreamDestination()

Creates a MediaStreamAudioDestinationNode associated with a MediaStream representing an audio stream which may be stored in a local file or sent to another computer.

AudioContext.createMediaStreamTrackSource()

Creates a MediaStreamTrackAudioSourceNode associated with a MediaStream representing an media stream track.

AudioContext.getOutputTimestamp()

Returns a new AudioTimestamp object containing two audio timestamp values relating to the current audio context.

AudioContext.resume()

Resumes the progression of time in an audio context that has previously been suspended/paused.

AudioContext.setSinkId() Experimental

Sets the output audio device for the AudioContext.

AudioContext.suspend()

Suspends the progression of time in the audio context, temporarily halting audio hardware access and reducing CPU/battery usage in the process.

Events

sinkchange Experimental

Fired when the output audio device (and therefore, the AudioContext.sinkId) has changed.

Examples

Basic audio context declaration:

js

const audioCtx = new AudioContext();

const oscillatorNode = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
const finish = audioCtx.destination;
// etc.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
AudioContext
35["Before Chrome 66, each tab is limited to 6 audio contexts in Chrome; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Chrome throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
14–57
12 25 No
22["Before Opera 53, each tab is limited to 6 audio contexts in Opera; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Opera throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
15–44
14.16
37["Before WebView 66, each tab is limited to 6 audio contexts in WebView; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, WebView throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
≤37–57
35["Before Chrome 66, each tab is limited to 6 audio contexts in Chrome; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Chrome throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
18–57
25
22["Before Opera Android 47, each tab is limited to 6 audio contexts in Opera; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Opera throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
14–43
14.56
3.0["Before Samsung Internet 9.0, each tab is limited to 6 audio contexts in Samsung Internet; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Samsung Internet throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
1.0–7.0
AudioContext 3514–57 12 25 No 2215–44 14.16–14.1 374.4–57 3518–57 25 2214–43 14.56–14.5 3.01.0–7.0
baseLatency 58 79 70 No 45 14.1 58 58 79 43 14.5 7.0
close 42 14 40 No 29 9 42 42 40 29 9 4.0
createMediaElementSource 15 12 25 No 15 6 ≤37 18 25 14 6 1.0
createMediaStreamDestination 25 79 25 No 15 11 ≤37 25 25 14 11 1.5
createMediaStreamSource 22 12 25 No 15 11 ≤37 25 25 14 11 1.5
createMediaStreamTrackSource No No
68Firefox 68 implements the updated standard's definition of the "first" audio track; now the first track is the one whose ID comes first lexicographically.
No No No No No
68Firefox 68 implements the updated standard's definition of the "first" audio track; now the first track is the one whose ID comes first lexicographically.
No No No
getOutputTimestamp 57 79 70 No 44 14.1 57 57 79 43 14.5 7.0
outputLatency 102 102 70 No 88 No 102 102 79 70 No 19.0
resume 41 14 40 No 28 9 41 41 40 28 9 4.0
setSinkId 110 110 No No 96 No 110 110 No 74 No 21.0
sinkId 110 110 No No 96 No 110 110 No 74 No 21.0
sinkchange_event 110 110 No No 96 No 110 110 No 74 No 21.0
suspend 41 14 40 No 28 9 41 41 40 28 9 4.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext