AudioContext

AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode . An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything happens inside a context. It's recommended to create one AudioContext and reuse it instead of initializing a new one each time, and it's OK to use a single AudioContext for several different audio source and pipeline concurrently.

构造函数

AudioContext()
Creates and returns a new AudioContext 对象。

特性

Also inherits properties from its parent interface, BaseAudioContext .

AudioContext.baseLatency 只读
Returns the number of seconds of processing latency incurred by the AudioContext passing the audio from the AudioDestinationNode to the audio subsystem.
AudioContext.outputLatency 只读
Returns an estimation of the output latency of the current audio context.

方法

Also inherits methods from its parent interface, BaseAudioContext .

AudioContext.close()
Closes the audio context, releasing any system audio resources that it uses.
AudioContext.createMediaElementSource()
创建 MediaElementAudioSourceNode associated with an HTMLMediaElement . This can be used to play and manipulate audio from <video> or <audio> elements.
AudioContext.createMediaStreamSource()
创建 MediaStreamAudioSourceNode associated with a MediaStream representing an audio stream which may come from the local computer microphone or other sources.
AudioContext.createMediaStreamDestination()
创建 MediaStreamAudioDestinationNode associated with a MediaStream representing an audio stream which may be stored in a local file or sent to another computer.
AudioContext.createMediaStreamTrackSource()
创建 MediaStreamTrackAudioSourceNode associated with a MediaStream representing an media stream track.
AudioContext.getOutputTimestamp()
返回新 AudioTimestamp object containing two audio timestamp values relating to the current audio context.
AudioContext.resume()
Resumes the progression of time in an audio context that has previously been suspended/paused.
AudioContext.suspend()
Suspends the progression of time in the audio context, temporarily halting audio hardware access and reducing CPU/battery usage in the process.

范例

Basic audio context declaration:

var audioCtx = new AudioContext();
					

Cross browser variant:

var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
var oscillatorNode = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
var finish = audioCtx.destination;
// etc.
					

规范

规范 状态 Comment
Web 音频 API
The definition of 'AudioContext' in that specification.
工作草案

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
AudioContext Chrome 35
35
14 — 57
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge ≤18 Firefox 25 IE 不支持 No Opera 22
22
15 — 44
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 6
Prefixed
6
Prefixed
Prefixed Implemented with the vendor prefix: webkit
WebView Android Yes Chrome Android 35
35
18 — 57
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 26 Opera Android 22
22
14 — 43
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 6
Prefixed
6
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Samsung Internet Android 3.0
3.0
1.0 — 7.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
AudioContext() 构造函数 Chrome 55
注意事项
55
注意事项
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 .
latencyHint isn't valid, Chrome throws a TypeError exception. See Non-standard exceptions in Chrome 了解细节。
Edge ≤18 Firefox 25 IE 不支持 No Opera 42 Safari Yes
Prefixed
Yes
Prefixed
Prefixed Implemented with the vendor prefix: webkit
WebView Android 55 Chrome Android 55
注意事项
55
注意事项
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 .
latencyHint isn't valid, Chrome throws a TypeError exception. See Non-standard exceptions in Chrome 了解细节。
Firefox Android 26 Opera Android 42 Safari iOS Yes
Prefixed
Yes
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Samsung Internet Android 6.0
注意事项
6.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 Samsung Internet .
latencyHint isn't valid, Samsung Internet throws a TypeError exception. See Non-standard exceptions in Samsung Internet 了解细节。
baseLatency
Chrome 58 Edge ≤79 Firefox 70 IE 不支持 No Opera 45 Safari 不支持 No WebView Android 58 Chrome Android 58 Firefox Android 不支持 No Opera Android 43 Safari iOS 不支持 No Samsung Internet Android 7.0
close Chrome 42 Edge 14 Firefox 40 IE 不支持 No Opera Yes Safari Yes WebView Android 43 Chrome Android 43 Firefox Android 40 Opera Android Yes Safari iOS Yes Samsung Internet Android 4.0
createConstantSource Chrome 不支持 14 — 56
Prefixed 注意事项
14 — 56
Prefixed 注意事项
Prefixed Implemented with the vendor prefix: webkit
Available as a part of BaseAudioContext .
Edge 不支持 ≤18 — 79 Firefox 不支持 25 — ?
注意事项
25 — ?
注意事项
Available as a part of BaseAudioContext .
IE 不支持 No Opera 不支持 22 — 43
注意事项
22 — 43
注意事项
Available as a part of BaseAudioContext .
15 — 22
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 不支持 6 — ?
Prefixed 注意事项
6 — ?
Prefixed 注意事项
Prefixed Implemented with the vendor prefix: webkit
Available as a part of BaseAudioContext .
WebView Android 不支持 ? — 56
注意事项
? — 56
注意事项
Available as a part of BaseAudioContext .
Chrome Android 不支持 18 — 56
注意事项
18 — 56
注意事项
Available as a part of BaseAudioContext .
Firefox Android 不支持 26 — ?
注意事项
26 — ?
注意事项
Available as a part of BaseAudioContext .
Opera Android 不支持 22 — 43
注意事项
22 — 43
注意事项
Available as a part of BaseAudioContext .
14 — 22
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 不支持 No Samsung Internet Android 不支持 1.0 — 6.0
注意事项
1.0 — 6.0
注意事项
Available as a part of BaseAudioContext .
createMediaElementSource Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
createMediaStreamDestination Chrome 14 Edge ≤18 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
createMediaStreamSource Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
createMediaStreamTrackSource Chrome 不支持 No Edge 不支持 No Firefox 68
注意事项
68
注意事项
Firefox 68 implements the updated standard's definition of the "first" audio track; now the first track is the one whose ID comes first lexicographically.
IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 68
注意事项
68
注意事项
Firefox 68 implements the updated standard's definition of the "first" audio track; now the first track is the one whose ID comes first lexicographically.
Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
getOutputTimestamp
Chrome 57 Edge ≤79 Firefox 70 IE 不支持 No Opera 44 Safari 不支持 No WebView Android 57 Chrome Android 57 Firefox Android 不支持 No Opera Android 43 Safari iOS 不支持 No Samsung Internet Android 7.0
outputLatency Chrome 不支持 No Edge 不支持 No Firefox 70 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
resume Chrome 41 Edge ≤18 Firefox 40 IE 不支持 No Opera Yes Safari Yes WebView Android Yes Chrome Android 41 Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android 4.0
suspend Chrome 43 Edge 14 Firefox 40 IE 不支持 No Opera Yes Safari Yes WebView Android 43 Chrome Android 43 Firefox Android 40 Opera Android Yes Safari iOS Yes Samsung Internet Android 4.0

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅