How do mobile websites pause videos when other apps start playing audio?

Viewed 263

Some mobile sites, like YouTube and Twitch, will pause html <video> elements if other apps (like Spotify, or a podcast player that puts media controls in the notifications) start to play audio.

Interestingly, these don't just take audio focus - they also stop playing if they can't obtain it. As an example, I'm using firefox for android, so I tried disabling its ability to take audio focus with adb:

cmd appops set org.mozilla.firefox TAKE_AUDIO_FOCUS ignore

But now, videos just immediately pause, since it can't pause the other audio source.

How do the sites detect this? I attached a debugger to my phone and looked through the docs but I didn't see anything in either place.

1 Answers

I'm not sure about how this specific flag "TAKE_AUDIO_FOCUS" is interpretted, but modern Android focus management is based on "requesting" (not taking) audio focus. Apps would request it and either get it immediately or listen for updates from the AudioManager as to whether they got it. Similarly they will get updates when someone else requests (and then subsequently receives) focus, and they should react accordingly (i.e. pause/duck themselves). Presumably the apps you mention have asked for audioFocus and were denied it and then hadn't received focus yet, so they just chose to stay paused rather than start playing audio/video and blare out over the app that hadn't released focus yet.

source: https://developer.android.com/guide/topics/media-apps/audio-focus

Related