Skip to content

fix(android): resume the last song on a cold-start play press instead of ANRing - #348

Open
BUR4KBEY wants to merge 1 commit into
ghenry22:masterfrom
BUR4KBEY:fix/android-media-button-cold-start-anr
Open

BUR4KBEY wants to merge 1 commit into
ghenry22:masterfrom
BUR4KBEY:fix/android-media-button-cold-start-anr

Conversation

@BUR4KBEY

@BUR4KBEY BUR4KBEY commented Dec 2, 2026

Copy link
Copy Markdown

Summary

On Android, a play press while Substreamer is not running (One UI's "Play last song" card, Bluetooth, a headset, Waze) did nothing, and about 40 s later Android raised an ANR: Context.startForegroundService() did not then call Service.startForeground(). Now the press resumes the last queue at the saved track and position, and when there is nothing to resume the service stops cleanly. Possibly the same bug as #274 (no logs there).

Cause: Media3's MediaButtonReceiver starts PlaybackService as a foreground service, but react-native-queue-player 3.0.0's onPlaybackResumption always returned an empty queue, so nothing played and startForeground was never called.

Changes

  • patches/react-native-queue-player+3.0.0.patch (the library repo is private, so this follows the existing patch-package convention):
    • New onPlaybackResumptionRequest callback (Nitro spec + regenerated bindings, registerPlaybackService handler field). Empty tracks means nothing to resume. Android-only; iOS accepts and ignores it.
    • PlaybackService: a cold media-button start calls startForeground with a placeholder on the media notification id at once, awaits the JS answer (cancellable, 20 s), loads it via applyResolvedAssistantQueue at the saved index, and hands it to Media3 to play. Nothing to resume: pause, stopForeground, stopSelf.
    • Nitro 0.37.1 note: Kotlin Promise.then throws on a null result and await() ignores cancellation, hence the non-optional result and awaitCancellable.
  • playerService: resolvePlaybackResumption answers from the persisted queue through the same restore code as the boot hydration (prepareRestoredQueue); a later UI boot in the same process adopts the resumed session instead of loading it again.
  • headlessMediaService: wires the handler; resumes nothing when logged out or while a Navidrome re-key is pending (narrowing to downloads would prune the saved queue).

Testing

  • npx tsc --noEmit passes
  • npx jest --no-coverage passes
  • New/updated tests added (Robolectric PlaybackServiceResumptionTest, PlaybackServiceLifecycleTest; jest playerService.hydration, headlessMediaService.voice)
  • Tested on iOS (not built: the iOS side is a no-op stub plus regenerated Nitro bindings)
  • Tested on Android: API 35 emulator against Navidrome (paused at 10.62 s, force-stop, cmd media_session dispatch play -> playing the same track from 10.66 s about 0.86 s later, no ANR after 65 s; cleared queue -> service stops after 0.7 s, no ANR; unpatched master ANRs at +40.7 s). Also on a Galaxy S25 (One UI 9) with the "Play last song" card.

Known gap (not new): a cold-start headset double-tap is turned into "skip next" by Media3 and never asks for resumption.

Created with Claude Opus 5.5 in omp (T3 Code). Review by a Claude Opus 5.5 omp subagent.

A media-button PLAY with the app not running (One UI "Play last song",
Bluetooth, Waze) makes Media3's MediaButtonReceiver call
startForegroundService on PlaybackService. RNQP's onPlaybackResumption
always returned an empty queue, so nothing played, startForeground was
never called and the system raised an ANR.

Patch react-native-queue-player with an onPlaybackResumptionRequest
handler that Media3's onPlaybackResumption awaits, using the same
cold-start pattern as the Assistant and Android Auto callbacks: the
headless JS boot registers the handler, which returns the saved queue,
track and position, and the library loads it for Media3 to play.

A cold media-button start calls startForeground with a placeholder on
the media notification id before waiting, so the service meets the
foreground contract however long the JS boot takes. When there is
nothing to resume (no saved queue, logged out, a pending Navidrome
re-key, nothing playable, no answer within the timeout) the player is
paused and the service stops. The answer is never optional and the
wait is cancellable: Nitro's Kotlin Promise cannot deliver a null
result and its await() ignores cancellation, so either would hang.

Substreamer answers from the persisted queue through the same restore
code as the boot hydration. Opening the app later in the same process
adopts the resumed session instead of loading the queue again.

This branch has not been deployed

No deployments
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.

1 participant