Suspended AudioContext after returning to a browser game

MusheghARMCP Posted messages 1 Registration date   Status Member Last intervention   -  

Hello,

I’m looking for a reliable strategy to resume sound after a visibility change in a browser game. The reproducible case is this page: https://armcp.net/angel-arena

Scenario:
1. the user starts a session and the AudioContext becomes « running »;
2. they switch tabs for a few seconds;
3. on return, the context sometimes remains « suspended »;
4. calling resume() from visibilitychange or focus isn’t always enough, because these events aren’t user gestures.

So I tried memorizing a resume request, then calling audioContext.resume() on the next pointerdown or keydown. That works better, but creates two difficulties: the sounds requested in the meantime must be ignored or queued, and a double trigger can occur when a pointerdown and a click follow each other.

What architecture would you recommend to centralize this state? Should one listen to visibilitychange, pageshow and focus only to mark « resume needed », then perform a single resume on the next user gesture? How to cleanly avoid the obsolete sound queue and double triggering without recreating the AudioContext?

Thanks for sharing your experiences on Chrome, Firefox and Safari.