BaseAudioContext: state プロパティ

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.

stateBaseAudioContext インターフェイスの読み取り専用プロパティで、現在の AudioContext の状態を返します。

文字列です。取りうる値は以下の通りです。

suspended

音声コンテキストは(AudioContext.suspend() によって)一時停止中です。

running

音声コンテキストは通常動作中です。

closed

音声コンテキストは(AudioContext.close() によって)閉じられています。

状態の変化の扱い

次のスニペットは AudioContext states のデモライブ実行で確認)の一部です。 onstatechange ハンドラーは、状態が変わるたびにコンソールにログを出力するために使われています。

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

iOS Safari における一時停止した再生の再開の状態

iOS の Safari では、ユーザーが(タブの切り替え、ブラウザーの最小化、画面のオフなどで)ページを離れると、音声コンテキストの状態が "interrupted" に変わり、再開させる必要があります。例えば次のようにします。

js
function play() {
  if (audioCtx.state === "interrupted") {
    audioCtx.resume().then(() => play());
    return;
  }
  // play() 関数の残り
}

仕様書

Specification
Web Audio API
# dom-baseaudiocontext-state

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
state

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報