AudioContext() 构造函数创建新 AudioContext object which represents an audio-processing graph, built from audio modules linked together, each represented by an AudioNode .

句法

var audioCtx = new AudioContext();
var audioCtx = new AudioContext(options);
					

参数

选项 可选
An object based on the AudioContextOptions dictionary that contains zero or more optional properties to configure the new context. Available properties are as follows:
latencyHint 可选
The type of playback that the context will be used for, as a value from the AudioContextLatencyCategory enum or a double-precision floating-point value indicating the preferred maximum latency of the context in seconds. The user agent may or may not choose to meet this request; check the value of AudioContext.baseLatency to determine the true latency after creating the context.
sampleRate 可选
sampleRate to be used by the AudioContext , specified in samples per second. The value may be any value supported by AudioBuffer . If not specified, the preferred sample rate for the context's output device is used by default.

返回值

The newly constructed AudioContext 实例。

异常

NotSupportedError
指定 sampleRate isn't supported by the context.

用法注意事项

The specification doesn't go into a lot of detail about things like how many audio contexts a user agent should support, or minimum or maximum latency requirements (if any), so these details can vary from browser to browser. Be sure to check the values if they matter to you.

In particular, the specification doesn't indicate a maximum or minimum number of audio contexts that must be able to be open at the same time, so this is left up to the browser implementations to decide.

Google Chrome

Per-tab audio context limitation in Chrome

Prior to version 66 Google Chrome only supported up to six audio contexts per tab at a time.

Non-standard exceptions in Chrome

If the value of the latencyHint property isn't valid, Chrome throws a TypeError exception with the message "The provided value '...' is not a valid enum value of type AudioContextLatencyCategory".

范例

This example creates a new AudioContext for interactive audio (optimizing for latency) and a sample rate of 44.1kHz.

var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext({
  latencyHint: 'interactive',
  sampleRate: 44100,
});
									

规范

规范 状态 注释
Web 音频 API
The definition of 'AudioContext()' in that specification.
工作草案 初始定义。

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
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 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 了解细节。
latencyHint option Chrome 60 Edge 79 Firefox 不支持 No IE 不支持 No Opera 47 Safari ? WebView Android 60 Chrome Android 60 Firefox Android 不支持 No Opera Android 44 Safari iOS ? Samsung Internet Android 8.0
sampleRate option Chrome 74 Edge 79 Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari Yes WebView Android 74 Chrome Android 74 Firefox Android ? Opera Android ? Safari iOS Yes Samsung Internet Android 11.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: