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

Handle audio context state of interrupted #928

Closed
wants to merge 1 commit into from

Conversation

nanek
Copy link
Contributor

@nanek nanek commented Apr 10, 2018

Using Chrome on iOS 11, if the screen is turned off the web audio context goes into an interrupted state. Using Howler, it never resumes and no audio plays after turning the screen back on. This updates the autoResume to also handle the interrupted state.

@filharvey
Copy link

Can this please get merged, this is causing games for FB Instant Games, to lose sound, if an ad is played, or a phone call is recieved.

@nanek
Copy link
Contributor Author

nanek commented May 5, 2018

Note, even after this I continued to encounter issues with audio after the screen was off then back on. After investigating, it seemed related to the autoResume after an autoSuspend, which autoResume could attempt to restart the AudioContext based on the timer and not a user touch event, which mobile browsers will ignore.

Therefore I'm currently disabling, autoSuspend with Howler.autoSuspend = false.

Howler appears to track the AudioContext state internally, separately from the actual state. At times this seemed to get out of sync. Therefore I am now calling Howler.ctx.resume() before attempting to play an audio file, if the Howler.ctx.state is suspended or interrupted. This is similar to what autoResume is attempting, however autoResume appears to use the Howler.state instead of the actual AudioContext state which could be different.

@goldfire
Copy link
Owner

goldfire commented May 9, 2018

@nanek Would you mind submitting that as a separate issue? I'm going to merge this shortly and wouldn't want to lose track of that.

@goldfire
Copy link
Owner

goldfire commented May 9, 2018

Oh sorry, I see what you are saying now. I'll have to look at this a bit more before merging.

@Rolandisimo
Copy link

Rolandisimo commented Jul 19, 2018

Have the same issue currently.

Tested this out on iPhone X (11.3), 6s and 7. Unfortunately, this doesn't fix the issue. Also, no interrupted state was received. Chrome and Safari

@rowild
Copy link

rowild commented Oct 19, 2018

Any progress in the merging here?

@tywang2006
Copy link

I believe i have the same issue here

alvov added a commit to alvov/howler.js that referenced this pull request Dec 24, 2018
@@ -406,7 +406,7 @@
if (self.state === 'running' && self._suspendTimer) {
clearTimeout(self._suspendTimer);
self._suspendTimer = null;
} else if (self.state === 'suspended') {
} else if (self.state === 'suspended' || self.state === 'interrupted') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems that this check will never be true since self.state doesn't really depend on the AudioContext state (apart from initialisation inside HowlerGlobal.prototype._setup) and seems unlikely to ever have the interrupted value.
One way to fix this would be to check the state of AudioContext directly, but the downside is that the code gets more ugly and complicated:

if (self.state === 'running' && self.ctx.state !== 'interrupted' && self._suspendTimer) {
  clearTimeout(self._suspendTimer);
  self._suspendTimer = null;
} else if (self.state === 'suspended' || self.state === 'running' && self.ctx.state === 'interrupted') {

Copy link
Contributor

Choose a reason for hiding this comment

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

@nanek I reread the discussion and it turns out that's what you already proposed :)

Copy link
Contributor

Choose a reason for hiding this comment

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

one more thing: in this case inside the play method the condition

if (Howler.state === 'running') {

needs to be rewritten as

if (Howler.state === 'running' && Howler.ctx.state !== 'interrupted') {

otherwise sounds will try to play before the context state is resumed

Copy link
Contributor

Choose a reason for hiding this comment

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

added this fix to this PR #1106

@hansagames
Copy link

@nanek @goldfire any news on this ? something need to be done still ?

@heand
Copy link

heand commented Oct 7, 2019

My issue is similar:
I have a wep app, added to home screen om my ipad. When i press home button on ipad (while a sound is playing with howler) the sounds stops. When I click the web app icon and brings the web page up again, the sound is gone. I then have to clear safari memory to get it working again, any ideas?

kant2002 pushed a commit to kant2002/howler.js that referenced this pull request Apr 14, 2020
@goldfire goldfire closed this in 67cd6ce May 17, 2020
goldfire added a commit that referenced this pull request May 17, 2020
Fix: Sounds stop playing after context is interrupted (fixes #928)
xifengxi added a commit to xifengxi/engine that referenced this pull request Jul 15, 2020
fix audio wich use player-web sometimes not work in wkwebview

场景:打包web-mobile,在ios的wkwebview中加载。
问题1:预加载音频时,this._context.state为running,但是播放的时候this._context.state为interrupted。
问题2:切到后台再切回,this._context.state为interrupted。
解决方案:如果在播放的时候,如果状态为interrupted,强制resume一下。
相关信息:goldfire/howler.js#928
pandamicro pushed a commit to cocos/cocos-engine that referenced this pull request Jul 19, 2020
fix audio wich use player-web sometimes not work in wkwebview

场景:打包web-mobile,在ios的wkwebview中加载。
问题1:预加载音频时,this._context.state为running,但是播放的时候this._context.state为interrupted。
问题2:切到后台再切回,this._context.state为interrupted。
解决方案:如果在播放的时候,如果状态为interrupted,强制resume一下。
相关信息:goldfire/howler.js#928
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants