fgs-state-ended-trap
The ended-state trap during a delegate swap
The "FGS" in the name is Android's foreground service — the status that keeps a media app alive in the background. When each track gets its own player object, the end of a track looks like this from the outside:
player A reports ended ──┐
│ ~25 ms
player B swapped in, plays ┘
For those few milliseconds the media session is asked for the playback state and answers ended.
Foreground status survives only while the player intends to play and the state is ready or
buffering — ended fails that test, so the service detaches its notification and stops being a
foreground service. (The state field is the one consulted, which is why the fix below remaps the
state rather than faking isPlaying.) On stock builds the app
usually survives long enough for player B to arrive. On the more aggressive power-management builds
some manufacturers ship, the app is frozen in that window and playback simply stops mid-queue.