Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AudioContext not being properly resumed on iOS 15. #4062

Merged
merged 8 commits into from
Mar 2, 2022

Conversation

jpauloruschel
Copy link
Contributor

@jpauloruschel jpauloruschel commented Feb 28, 2022

Fixes #3977

Fix issues on the AudioContext not being properly resumed, especially on iOS 15. This is done by explicitly calling .resume() on the AudioContext when required. Extra checks were added to make sure resume is only called when the window is being re-focused, and the state is going from suspended or interrupted back to running - otherwise exceptions are thrown. Additionally, if for any reason the context can't be resumed successfully, the Auto-Play event listeners are re-bound so that the audio can be resumed later.

Documentation:

As far as I could test, this new code behaves well with all of our current target devices and browsers, but we could also consider using external libraries for this kind of stuff, such as the ones below (pending analysis, of course).

New test scenarios in Safari iOS 15:

  • Launch game with audio, change Tabs (audio stops), change back to game (audio resumes)
  • Launch game with audio, close Safari (audio stops), wait some time (from immediate to around 40 seconds), open Safari (audio resumes)
  • Launch game with audio, wait for 30 second auto-lock (audio stops), wait some time (from immediate to around 40 seconds), unlock phone (audio resumes)
  • Launch game with audio, close browser, lock phone. Go back several minutes later: requires interaction due to auto-play policy.

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

src/sound/manager.js Outdated Show resolved Hide resolved
src/sound/manager.js Outdated Show resolved Hide resolved
src/sound/manager.js Outdated Show resolved Hide resolved
src/sound/manager.js Outdated Show resolved Hide resolved
willeastcott
willeastcott previously approved these changes Feb 28, 2022
Copy link
Contributor

@willeastcott willeastcott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really cool to see this fixed! 👏

@mvaligursky mvaligursky added the release: next patch Ticket marked for next patch release label Mar 1, 2022
@jpauloruschel jpauloruschel force-pushed the jpaulo-fix-ios-audio-from-sleep branch from d74c8ac to 2c1dacd Compare March 1, 2022 16:53
@jpauloruschel jpauloruschel dismissed willeastcott’s stale review March 1, 2022 16:54

Breaking changes were added which require another review.

src/sound/manager.js Outdated Show resolved Hide resolved
src/sound/manager.js Outdated Show resolved Hide resolved
@mvaligursky
Copy link
Contributor

Please test this on IE11 as well .. as that could behave differently too (if not already)

@jpauloruschel jpauloruschel force-pushed the jpaulo-fix-ios-audio-from-sleep branch from 4541e7d to 094f154 Compare March 2, 2022 12:17
src/sound/manager.js Outdated Show resolved Hide resolved
src/sound/manager.js Outdated Show resolved Hide resolved
src/sound/manager.js Outdated Show resolved Hide resolved
Copy link
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work. Anybody else wants to have a look?

Copy link
Member

@slimbuck slimbuck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l approve of this!

this._resumeContext = () => {
if (!this.context || this.context.state === 'running') {
USER_INPUT_EVENTS.forEach((eventName) => {
window.removeEventListener(eventName, this._resumeContext);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inception. i like.

@jpauloruschel jpauloruschel merged commit 0d82af7 into dev Mar 2, 2022
@jpauloruschel jpauloruschel deleted the jpaulo-fix-ios-audio-from-sleep branch March 2, 2022 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: audio bug release: next patch Ticket marked for next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iOS 15 sound does not resume when woken from sleep
4 participants