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
var audioCtx = new AudioContext(); var audioCtx = new AudioContext(options);
Parameters
options
Optional- An object containing option properties that can be set when creating the object instance. Available properties are as follows:
latencyHint
: A string or double value that identifies type of playback, affecting tradeoffs between audio output latency and power consumption. The default value isinteractive
. Although finer control can be achieved by passing a double value (indiciating number of seconds of latency), the preferred values are as follows:balanced
, balances audio output latency and power consumption.interactive
, provides lowest audio output latency as possible without glitching.playback
, prioritizes sustained playback without interruption over audio output latency.
sampleRate
: A float with the requested sampling rate for this audio context, between 8,000 and 96,000 Hz. The default value depends on the audio output device but in most cases it is 44,100 Hz.
Return value
The newly constructed AudioContext
object instance.
Exceptions
- In Google Chrome, the number of audio contexts in each tab is limited because each audio context spawns a new thread. If you create more than six audio contexts in one tab, Google Chrome will throw a
DOMException
(The number of hardware contexts provided (6) is greater than or equal to the maximum bound (6)). Other browsers do not have this restriction.
Therefore, try to reuse the same audio context in your application, instead of creating a new audio context for each sound. If you no longer need an audio context, you can callAudioContext.close()
. To be sure that the audio context was deleted, you must wait until thePromise
is resolved but in practice this only takes a few milliseconds. - In Google Chrome, throws a
TypeError
iflatencyHint
has an invalid value (The provided value '...' is not a valid enum value of type AudioContextLatencyCategory). - According to the spec, a
NotSupportedError
will be thrown ifsampleRate
is not supported by the hardware, however no browser currently implements this parameter.
Example
var AudioContext = window.AudioContext || window.webkitAudioContext; var audioCtx = new AudioContext({ latencyHint: 'interactive', sampleRate: 44100, });
Specifications
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'AudioContext()' in that specification. |
Working Draft | Initial definition. |
Browser Compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 55 | Yes | 25 | No | 42 | Yes webkit |
latencyHint option | 60 | No | No | No | 47 | ? |
sampleRate option | No1 | No | No | No | No | ? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | 55 | 55 | ? | 25 | No | 42 | ? |
latencyHint option | 60 | 60 | ? | No | No | 47 | ? |
sampleRate option | ? | ? | ? | ? | ? | ? | ? |
1. See issue 432248 for Chrome support.
See also
new OfflineAudioContext()
constructor
Document Tags and Contributors
Tags:
Contributors to this page:
Jedipedia,
fscholz,
CornerKitten,
teoli,
chrisdavidmills,
jpmedley,
Sheppy,
bunnybooboo,
chaser,
chikoski
Last updated by:
Jedipedia,