Repository navigation
Description
Re-opening this issue. suspend() and resume() are indeed important parts of this proposal, but the "interrupted" state is important as well. During a phone call, (or other period of exclusive use of the audio hardware) requests to resume() the AudioContext will always fail. An "interrupted" state signals to the web page that, as opposed to merely "suspended", all requests to resume() will reject.
Additionally, "interrupted" is a state provided by the UA, rather than script running in the page. A UA could move the state from "running" to "interrupted" to "running" again (if the interruption allows normal audio playback to resume afterwards), or from "running" to "interrupted" to "suspended" (if audio playback is not allowed to resume automatically). In the first case, the page is free to react to the "interrupted" state by pausing (e.g.) the video game, and un-pause when the state moves to "running" after the interruption ends. In the second case, when the state moves to "suspended", the page can provide UI to restart playback (and the game) after the interruption. In either case, the page can choose to continue silently during the interruption.
"closed" is not a substitute here, as there is no mechanism for moving from "closed" to "running". "suspended" is not a substitute because there's no indication to the page why the state moved to "suspended" (did the user pause audio through UA provided hardware controls?) nor is there an indication when playback would be allowed again. The promise returned by "resume" is not a substitute, because if not immediately rejected, it would resolve at the exact same time as the UA would automatically move the state from "interrupted" to "running" in the first scenario, and would reject in the second without indicating to the page that, if they just tried again, things would work fine.
Originally posted by @jernoble in #72 (comment)
Activity
rtoy commentedon Jan 21, 2021
See also the followup comments:
Summary: General agreement that we need this, but there are quite a few questions on what the state transitions are and what causes the transitions. These need to be finalized before we can update the spec.
jernoble commentedon Jan 21, 2021
That's correct.
resume(), would reject, butsuspend()would (all other things being equal) would resolve. There's some wiggle room here;suspend()could also just not resolve until the interruption was over, and then move the state to"suspended", regardless of whether the system allowed playback to resume or not. Orsuspend()could resolve immediately and move the state to"suspended", andresume()could block until the interruption was over. I think there's a valid developer usability question here.Please keep in mind that it's actually the user that's responsible for the interruption. The user has received a phone call and the user is talking on the phone. The user has switched to another app and the user is using the audio hardware there. The user is recording a video and saving it to the user's photo library.
So to put it another way, the page must wait until the user allows the page to resume audio, mediated by the User Agent.
Yes, and this would cause the state to move to
"closed".I'd hate to freely expose to every web page the current state of whether the user is on the phone or not, so I'd argue to only move to the "interrupted" state from "running".
guest271314 commentedon Jan 22, 2021
guest271314 commentedon Jan 22, 2021
jernoble commentedon Jan 22, 2021
You seem to be suggesting that sites which care could implement some kind of "mute" behavior themselves. That's not the intent of this API; it's not the expectation that the User Agent will indicate to the page that the user is doing something else and the page should kindly mute itself. Rather, this is telling the page that audio playback will not continue, and it can take some action in response, like pausing the game being played, suspending an animation, etc.
On the platforms I am personally familiar with, this will not succeed. That new context will also enter the "interrupted" state, as that second context cannot get access to the audio hardware.
guest271314 commentedon Jan 23, 2021
guest271314 commentedon Jan 23, 2021
rtoy commentedon Feb 4, 2021
Teleconf: set priority to 2 for now.
rtoy commentedon May 19, 2021
Virtual F2F 2021: We agree that having an "interrupted" state makes sense. However, we need more details on exactly how this is intended to work to be able to specify this properply.
Christoph Guttandin mentioned that Safari sets the interrupted state if an audiocontext is silent and you switch tabs. When you switch back, it resumes. But if the context is playing, this doesn't happen. This all needs to be clarified.
hoch commentedon Sep 13, 2023
2023 TPAC Audio WG Discussion:
The group has not reached a conclusion yet, but we will check what WebKit does for the interrupted status and consider finding a common ground with the AudioSession API proposal.
chrisguttandin commentedon Sep 19, 2023
I build a very simple demo which plays a sine wave and logs the
stateof theAudioContext. It also allows to select theAudioSessionType(if supported) to check if that makes any difference.https://stackblitz.com/edit/js-3w4eqh
As far as I can tell the audio stops in any case on iOS 16.6 when the screen gets switched off. The
stateof theAudioContextwill then be'interrupted'.However there are subtle differences. When the
typeof theAudioSessionis'auto'or'playback'theAudioContextgets resumed automatically when the screen is turned back on. That doesn't seem to happen when thetypeof theAudioSessionis'transient'. In that case thestateof theAudioContextis'suspended'.@jyavenard Is that expected or should I file a bug for that?
jyavenard commentedon Sep 19, 2023
It sounds like https://bugs.webkit.org/show_bug.cgi?id=261554
and yes, there’s an issue as the behaviour isn’t consistent between macOS and iOS.
But only playback state will allow audio to be played while the page is suspended. Similar to how an audio/video element work
guest271314 commentedon Sep 20, 2023
gabrielsanbrito commentedon Aug 27, 2024
Hello! I have written an explainer to make progress on the
"interrupted"state proposal. I tried to incorporate as much as possible this issue's discussion into the document. Moreover, I am aware that WebKit has this implemented, but I am not sure to what degree the proposal is interoperable with it. I appreciate any feedback you may have. Thanks!mjwilson-google commentedon May 12, 2025
I think this was fixed by #2611 -- is there anything else we need to do here?
gabrielsanbrito commentedon May 12, 2025
@mjwilson-google I believe that we can close this.