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, e.g. 44100 or 48000. The default value is not mentioned in the specification, but in most browsers it is 44100.

Exceptions

  • In Google Chrome, this constructor will throw a DOMException if you try to create more than six audio contexts in one tab (The number of hardware contexts provided (6) is greater than or equal to the maximum bound (6)). Therefore, try to reuse the same audio context in the whole application, as opposed to creating a new audio context for every audio element. If you no longer need an audio context, you can call AudioContext.close() but it will take some time until the audio context is actually deleted.
  • 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. See issue 432248 for Chrome support.

Example

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 ?
latencyHint option60 No No No47 ?
sampleRate option No No No ? ? ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic support5555 ?25 No42 ?
latencyHint option6060 ? No No47 ?
sampleRate option ? ? ? ? ? ? ?

See also

Document Tags and Contributors

 Last updated by: Jedipedia,