[expo-audio] Android drops audio focus on a transient rebuffer and never re-requests it, so playback talks over phone calls
Summary
AudioModule.kt releases audio focus on any transition to not-playing, including the transient one ExoPlayer emits when a buffer empties mid-stream. Focus is only ever requested from the play Function, and a self-healing rebuffer never re-enters it — so after the first rebuffer the player keeps playing with no audio focus and no registered OnAudioFocusChangeListener, for the rest of the session.
player.onPlaybackStateChange = { isPlaying ->
if (!isPlaying && shouldReleaseFocus()) {
releaseAudioFocus()
}
}onPlaybackStateChange is invoked from BaseAudioPlayer's Player.Listener.onIsPlayingChanged, which ExoPlayer fires on both edges of a rebuffer. shouldReleaseFocus() reads raw isPlaying across the registry, so during the buffering window nothing is playing and focus is released.
Why it is user-visible
Audio focus is what makes playback yield to an incoming call, a navigation prompt, or another media app. Once it has been dropped this way, the app talks straight over all of them — which is the one thing audio focus exists to prevent. For a long-running stream (a scanner, a radio app, a podcast on a flaky connection) a rebuffer is routine, so this is reached quickly and then persists.
Repro
- Start playback of a network stream.
- Force a brief rebuffer — airplane mode on for ~8 s, then off. Let the audio recover on its own.
(If the app shows a full reconnect instead, the gap was too long: a reconnect re-enters
play()and re-requests focus, masking the bug. Use a shorter gap.) - Have someone call the phone.
Expected: playback pauses (or ducks) for the ring and the call. Actual: playback continues over the ringtone and the entire call.
Fix
Treat STATE_BUFFERING as transient and don't release on it:
player.onPlaybackStateChange = { isPlaying ->
val transient = player.ref.playbackState == Player.STATE_BUFFERING
if (!isPlaying && !transient && shouldReleaseFocus()) {
releaseAudioFocus()
}
}STATE_ENDED is deliberately still treated as a real stop.
Related, same area
The notification / lock-screen Play button never requests focus at all. media3 routes COMMAND_PLAY_PAUSE through MetadataInjectingPlayer → ForwardingPlayer → ExoPlayer directly, bypassing the play Function where the focus request lives — so the same control behaves differently in-app and from the notification. The tidier fix for both is probably to let ExoPlayer own focus via setAudioAttributes(attrs, handleAudioFocus = true) and drop the manual bookkeeping, but that is a larger change and running both would double-request, so I kept it out of the patch above.
Environment
[email protected], Expo SDK 57, react-native 0.86.2, bare workflow- Android 16 emulator (API 36),
targetSdk/compileSdk36 - Verified with
adb shell dumpsys audiobefore/after a rebuffer
Happy to open a PR if the approach looks right.
Source: expo/expo