-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
66 lines (48 loc) · 1.69 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
title: "BaseAudioContext: state property"
short-title: state
slug: Web/API/BaseAudioContext/state
page-type: web-api-instance-property
browser-compat: api.BaseAudioContext.state
---
{{ 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.)
- `running`
- : The audio context is running normally.
- `closed`
- : The audio context has been closed (with the
{{domxref("AudioContext.close()")}} method.)
## Examples
### Handling state changes
The following snippet is taken from our [AudioContext states demo](https://github.com/mdn/webaudio-examples) ([see it running live](https://mdn.github.io/webaudio-examples/audiocontext-states/).) The {{domxref("BaseAudioContext.statechange_event", "onstatechange")}} handler is used to log the
current state to the console every time it changes.
```js
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:
```js
function play() {
if (audioCtx.state === "interrupted") {
audioCtx.resume().then(() => play());
return;
}
// rest of the play() function
}
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Using the Web Audio API](/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API)