I'm running into an issue where calling resume
on an AudioContext
never resolves when attempting to play audio in Safari. I'm creating an AudioContext
on page load, thus it starts in a suspended state.
According to this chromium issue, calling resume
will not resolve if blocked by the browser's autoplay policy. I have a click event bound to a <button>
that will resume the context if it's suspended. This works in both Firefox and Chrome, and will work in Safari if I change the autoplay settings for the site.
Below is how I would normally resume the context:
await context.resume();
For Safari, I've tried calling resume without waiting for the promise to resolve, and instead register a callback for onstatechange
, which is then wrapped in a Promise
:
if (window.webkitAudioContext) {
await new Promise((accept, reject) => {
this.context.onstatechange = async () => {
if ((await this.context.state) === "playing") {
accept();
} else {
reject();
}
};
this.context.resume();
});
}
However, nothing has changed: the Promise
never resolves, which means that the context state isn't changing.
Am I missing something?
resume()
never resolves but your workaround never resolves because it checks for a wrong value.state
will be'running'
when the context was resumed successfully. It also is a regular property which does not need to be awaited. The fourth line would then beif (this.context.state === "running") {
.