AudioContext.onstatechange

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

AudioContextonstatechange 属性定义了一个事件处理器函数,触发 statechange 会被调用,也就是说 audio context 的状态发生变化时会执行。

语法

js
var audioCtx = new AudioContext();
audioCtx.onstatechange = function() { ... };

例子

下面这段代码是AudioContext states DEMO (直接运行) 中的,其中 onstatechange 处理器会在每次当前 state 发生变化时把它输出到控制台。

js
audioCtx.onstatechange = function () {
  console.log(audioCtx.state);
};

规范

Specification
Web Audio API
# dom-baseaudiocontext-onstatechange

浏览器兼容性

BCD tables only load in the browser

参见