engine icon indicating copy to clipboard operation
engine copied to clipboard

Fix AudioContext not resuming properly.

Open jpauloruschel opened this issue 2 years ago • 0 comments

Fixes https://github.com/playcanvas/engine/issues/3977

Summary

Fix AudioContext not resuming properly in Mobile devices. Previously, the AudioContext often broke when minimising / switching tabs and going back to the page. Once broken, no Audio could be played, and the only resolution was reloading the entire page. This PR addresses this by re-writing the entire SoundManager suspend / resume flow.

I've added documentation to the code, but basically:

  • If AudioContext is locked (verifiable by checking if the AudioContext.state is suspended right after creation), all SoundInstances will wait until the Context is unlocked to actually start playing;
  • Once unlocked, SoundManager.resume() (called manually or automatically on visibilitychange) will call audioContext.resume() if state was interrupted, and SoundInstances will only resume once that call was successful;
  • Once unlocked, every time the AudioContext's state chages (onstatechange), an additional check will be made to resume the AudioContext.

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

Testing

Test project using custom engine with changes: https://playcanv.as/p/nwJQKFeE/

Tested the following situations on iPhone Chrome and Safari:

  • Test 1
    1. Open page
    2. Tap to start playing audio
    3. Minimize app, wait 30 seconds
    4. Back to app, audio resumes immediately
  • Test 2
    1. Open page
    2. Minimize app, wait 30 seconds
    3. Back to app, audio doesn't resume (still locked)
    4. Tap to unlock, audio plays
  • Test 3
    1. Open page
    2. Tap to start playing audio
    3. Minimize app, turn off screen, wait 30 seconds
    4. Turn screen back on, back to app, audio resumes immediately

Tested the following situations on MacBook Chrome and Safari:

  • Test 1
    1. Open page
    2. Click to start playing audio
    3. Minimize app, wait 30 seconds
    4. Back to app, audio resumes immediately
  • Test 2
    1. Open page
    2. Minimize app, wait 30 seconds
    3. Back to app, audio doesn't resume (still locked)
    4. Tap to unlock, audio plays

Tested the following situtation on MacBook & iPhone Chrome & Safari:

Screenshot 2022-08-01 at 14 10 18

(Available here: https://playcanv.as/p/nwJQKFeE/) Wait for tank to pass through each sphere - which performs the action in the text. This is done so there's no user interaction which would unlock the page - this way we can test what happens when context is unlocked in various stages.

jpauloruschel avatar Jul 21 '22 14:07 jpauloruschel