BaseAudioContext.state
 The state read-only property of the BaseAudioContext interface returns the current state of the AudioContext. 
Value

A DOMString. Possible values are:   suspended: The audio context has been suspended (with the AudioContext.suspend() method.)  
running: The audio context is running normally.  closed: The audio context has been closed (with the AudioContext.close() method.)  

Examples
Handling state changes

 The following snippet is taken from our AudioContext states demo (see it running live.) The onstatechange handler is used to log the current state to the console every time it changes.  audioCtx.onstatechange = function() {
  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

Specification
Web Audio API # dom-baseaudiocontext-state

Browser compatibility




Desktop
Mobile



Chrome
Edge
Firefox
Internet Explorer
Opera
Safari
WebView Android
Chrome Android
Firefox for Android
Opera Android
Safari on IOS
Samsung Internet



state
41
14
40
No
28
9
41
41
40
28
9
4.0


See also
 Using the Web Audio API 


Found a problem with this page?

Edit on GitHub
Source on GitHub
Report a problem with this content on GitHub
Want to fix the problem yourself? See our Contribution guide.


Last modified: Mar 27, 2022, by MDN contributors


    
{'path': 'public/docs/dom/baseaudiocontext/state.html', 'source': 'DevDocs.IO'}