BaseAudioContext: state property
{{ APIRef("Web Audio API") }}
The state
read-only property of the {{ domxref("BaseAudioContext") }}
interface returns the current state of the AudioContext
.
Value
A string. Possible values are:
suspended
- : The audio context has been suspended (with the
{{domxref("AudioContext.suspend()")}}
method.)
- : The audio context has been suspended (with the
running
- : The audio context is running normally.
closed
- : The audio context has been closed (with the
{{domxref("AudioContext.close()")}}
method.)
- : The audio context has been closed (with the
Examples
Handling state changes
The following snippet is taken from our AudioContext states demo (see it running live.) The {{domxref("BaseAudioContext.statechange_event", "onstatechange")}}
handler is used to log the
current state to the console every time it changes.
audioCtx.onstatechange = () => {
console.log(audioCtx.state);
};
Resuming interrupted play states in iOS Safari
In iOS Safari, when a user leaves the page (e.g., switches tabs, minimizes the browser, or turns off the screen) the audio context’s state changes to “interrupted” and needs to be resumed. For example:
function play() {
if (audioCtx.state === "interrupted") {
audioCtx.resume().then(() => play());
return;
}
// rest of the play() function
}
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}