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 is interactive. 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 call AudioContext.close(). To be sure that the audio context was deleted, you must wait until the Promise is resolved but in practice this only takes a few milliseconds.
  • In Google Chrome, throws a TypeError if latencyHint 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 if sampleRate 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

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support55 Yes25 No42 Yes webkit
latencyHint option60 No No No47 ?
sampleRate option No1 No No No No ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support5555 ?2542 ?6.0
latencyHint option6060 ? No47 ? No
sampleRate option ? ? ? ? ? ? No

1. See issue 432248 for Chrome support.

See also

Document Tags and Contributors

 Last updated by: Jedipedia,