--- title: AudioContext.onstatechange slug: Web/API/BaseAudioContext/onstatechange translation_of: Web/API/BaseAudioContext/onstatechange original_slug: Web/API/AudioContext/onstatechange ---

{{ APIRef("Web Audio API") }}

{{ domxref("AudioContext") }}的onstatechange属性定义了一个事件处理器函数,触发{{Event("statechange")}}会被调用,也就是说audio context的状态发生变化时会执行。

语法

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

例子

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

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

规范

Specification Status Comment
{{SpecName('Web Audio API', '#widl-AudioContext-onstatechange', 'onstatechange')}} {{Spec2('Web Audio API')}}  

浏览器兼容性

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatChrome(43.0)}} {{CompatGeckoDesktop(40.0)}}  {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

另见