W3cubDocs

/DOM

AudioContext.constructor

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

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

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/AudioContext