AudioContext: AudioContext() constructor

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

* Some parts of this feature may have varying levels of support.

The AudioContext() constructor creates a new AudioContext object which represents an audio-processing graph, built from audio modules linked together, each represented by an AudioNode.

Syntax

js
new AudioContext()
new AudioContext(options)

Parameters

options Optional

An object used to configure the context. The available properties are:

latencyHint Optional

The type of playback that the context will be used for, as a predefined string ("balanced", "interactive" or "playback") or a double-precision floating-point value indicating the preferred maximum latency of the context in seconds. The user agent may or may not choose to meet this request; check the value of AudioContext.baseLatency to determine the true latency after creating the context.

  • "balanced": The browser balances audio output latency and power consumption when selecting a latency value.
  • "interactive" (default value): The audio is involved in interactive elements, such as responding to user actions or needing to coincide with visual cues such as a video or game action. The browser selects the lowest possible latency that doesn't cause glitches in the audio. This is likely to require increased power consumption.
  • "playback": The browser selects a latency that will maximize playback time by minimizing power consumption at the expense of latency. Useful for non-interactive playback, such as playing music.
sampleRate Optional

Indicates the sample rate to use for the new context. The value must be a floating-point value indicating the sample rate, in samples per second, for which to configure the new context; additionally, the value must be one which is supported by AudioBuffer.sampleRate. The value will typically be between 8,000 Hz and 96,000 Hz; the default will vary depending on the output device, but the sample rate 44,100 Hz is the most common. If the sampleRate property is not included in the options, or the options are not specified when creating the audio context, the new context's output device's preferred sample rate is used by default.

sinkId Optional

Specifies the sink ID of the audio output device to use for the AudioContext. This can take one of the following value types:

  • A string representing the sink ID, retrieved for example via the deviceId property of the MediaDeviceInfo objects returned by MediaDevices.enumerateDevices().
  • An object representing different options for a sink ID. Currently, this takes a single property, type, with a value of none. Setting this parameter causes the audio to be processed without being played through any audio output device.

Return value

A new AudioContext instance.

Exceptions

NotSupportedError DOMException

Thrown if the specified sampleRate isn't supported by the context.

Usage notes

The specification doesn't go into a lot of detail about things like how many audio contexts a user agent should support, or minimum or maximum latency requirements (if any), so these details can vary from browser to browser. Be sure to check the values if they matter to you.

In particular, the specification doesn't indicate a maximum or minimum number of audio contexts that must be able to be open at the same time, so this is left up to the browser implementations to decide.

Google Chrome

Per-tab audio context limitation in Chrome

Prior to version 66 Google Chrome only supported up to six audio contexts per tab at a time.

Non-standard exceptions in Chrome

If the value of the latencyHint property isn't valid, Chrome throws a TypeError exception with the message "The provided value '...' is not a valid enum value of type AudioContextLatencyCategory".

Example

This example creates a new AudioContext for interactive audio (optimizing for latency), with a sample rate of 44.1kHz and a specific audio output.

js
const audioCtx = new AudioContext({
  latencyHint: "interactive",
  sampleRate: 44100,
  sinkId: "bb04fea9a8318c96de0bd...", // truncated for brevity
});

Specifications

Specification
Web Audio API
# dom-audiocontext-audiocontext

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
AudioContext() constructor
Chrome – No support
Chrome 14 – 56 (Release date: 2011-09-16)
prefix
footnote Removed in 57 and later
prefix Implemented with the vendor prefix: webkit
Chrome – Full support
Chrome 35 (Release date: 2014-05-20)
footnote
footnote 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.
footnote If latencyHint isn't valid, Chrome throws a TypeError exception. See Non-standard exceptions in Chrome for details.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 25 (Release date: 2013-10-29)
footnote Full support
Opera – No support
Opera 15 – 43 (Release date: 2013-07-02)
prefix
footnote Removed in 44 and later
prefix Implemented with the vendor prefix: webkit
Opera – Full support
Opera 22 (Release date: 2014-06-03)
footnote
footnote 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.
footnote If latencyHint isn't valid, Opera throws a TypeError exception. See Non-standard exceptions in Chrome for details.
Safari – Full support
Safari 6 (Release date: 2012-07-25)
prefix
prefix Implemented with the vendor prefix: webkit
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote
footnote New audio contexts are suspended until the resume() method is called via user action, such as the click event.
Chrome Android – No support
Chrome Android 18 – 56 (Release date: 2012-06-27)
prefix
footnote Removed in 57 and later
prefix Implemented with the vendor prefix: webkit
Chrome Android – Full support
Chrome Android 35 (Release date: 2014-05-20)
footnote
footnote Before Chrome Android 66, each tab is limited to 6 audio contexts in Chrome Android; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome Android.
footnote If latencyHint isn't valid, Chrome Android throws a TypeError exception. See Non-standard exceptions in Chrome Android for details.
Firefox for Android – Full support
Firefox for Android 25 (Release date: 2013-10-29)
footnote Full support
Opera Android – No support
Opera Android 14 – 42 (Release date: 2013-05-21)
prefix
footnote Removed in 43 and later
prefix Implemented with the vendor prefix: webkit
Opera Android – Full support
Opera Android 22 (Release date: 2014-06-17)
footnote
footnote 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.
footnote If latencyHint isn't valid, Opera throws a TypeError exception. See Non-standard exceptions in Chrome for details.
Safari on iOS – Full support
Safari on iOS 6 (Release date: 2012-09-10)
prefix
prefix Implemented with the vendor prefix: webkit
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote
footnote New audio contexts are suspended until the resume() method is called via user action, such as the click event.
Samsung Internet – No support
Samsung Internet 1 – 6.4 (Release date: 2013-04-27)
prefix
footnote Removed in 7 and later
prefix Implemented with the vendor prefix: webkit
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote
footnote 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.
footnote If latencyHint isn't valid, Samsung Internet throws a TypeError exception. See Non-standard exceptions in Chrome for details.
WebView Android – No support
WebView Android 4.4 – 56 (Release date: 2013-12-09)
prefix
footnote Removed in 57 and later
prefix Implemented with the vendor prefix: webkit
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote
footnote Before WebView Android 66, each tab is limited to 6 audio contexts in WebView Android; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in WebView Android.
footnote If latencyHint isn't valid, WebView Android throws a TypeError exception. See Non-standard exceptions in WebView Android for details.
WebView on iOS – Full support
WebView on iOS 6 (Release date: 2012-09-10)
prefix
prefix Implemented with the vendor prefix: webkit
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote
footnote New audio contexts are suspended until the resume() method is called via user action, such as the click event.
options.latencyHint parameter
Chrome – Full support
Chrome 58 (Release date: 2017-04-19)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 45 (Release date: 2017-05-10)
footnote Full support
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – Full support
Chrome Android 58 (Release date: 2017-04-25)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – Full support
WebView Android 58 (Release date: 2017-04-25)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
options.sampleRate parameter
Chrome – Full support
Chrome 74 (Release date: 2019-04-23)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 61 (Release date: 2018-06-26)
footnote Full support
Opera – Full support
Opera 62 (Release date: 2019-06-27)
footnote Full support
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – Full support
Chrome Android 74 (Release date: 2019-04-24)
footnote Full support
Firefox for Android – Full support
Firefox for Android 61 (Release date: 2018-06-26)
footnote Full support
Opera Android – Full support
Opera Android 53 (Release date: 2019-07-11)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – Full support
Samsung Internet 11 (Release date: 2019-12-05)
footnote Full support
WebView Android – Full support
WebView Android 74 (Release date: 2019-04-24)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
options.sinkId parameter
Experimental
Chrome – Full support
Chrome 110 (Release date: 2023-02-07)
footnote Full support
Edge – Full support
Edge 110 (Release date: 2023-02-09)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 96 (Release date: 2023-02-22)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 110 (Release date: 2023-02-07)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 74 (Release date: 2023-03-13)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Full support
WebView Android – Full support
WebView Android 110 (Release date: 2023-02-07)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also