skip to content

As Android lead on a Media3 app, would you standardise on one player owned by a MediaSessionService or a player per screen, and what does each cost?

level: principalimportance: should knowfreq 22%

answer

  1. who owns the decoder and the release
  2. does playback outlive the screen
  3. controller versus direct calls
  4. one session per app
  5. split by content, not by screen

basics

~20 s

Default to one ExoPlayer owned by a MediaSessionService for content that must outlive the screen, such as lesson audio, with screens driving it through a MediaController. Keep per-screen players only for short previews that never play in the background.

solid answer

~50 s

I decide by whether playback must outlive the screen. A service-owned player with a `MediaSession` gives background play, lock-screen and Bluetooth controls, one decoder set and one place that calls `release()`, and every screen, including a mini player, attaches through a `MediaController`. Its cost is the service lifecycle: an asynchronous controller connection, foreground-service and notification behaviour, `onTaskRemoved` policy, and controllers that keep the service alive if a screen forgets to release them. A player per screen is simpler: synchronous, released in `onStop`, easy to test, but playback dies with the screen and two screens can hold two decoders. For the language-learning app I standardise on the service for lessons and allow local players only for muted previews with no session, and I keep a single session per app, because extra sessions show the app several times on car and Bluetooth displays.

go deeper

for a junior

Recall that a player can live in a screen or in a MediaSessionService, and that only the service keeps playing when the screen goes away.

for a middle

Explain how screens reach a service-owned player through MediaController and why the controller must be released with the screen.

for a senior

Weigh the concrete failure modes: rebuffering on navigation, duplicate sessions, leaked controllers that keep the service alive, and decoder pressure from parallel players.

for a principal

Set the default by content type, document the exceptions, and enforce one owner per player with review rules and a lock-screen playback test.

## The decision Every Media3 app eventually answers one question: **who owns the `ExoPlayer`?** Either one long-lived player sits in a `MediaSessionService` and screens reach it through a `MediaController`, or each screen builds, uses and releases its own player. There is no universally right answer, and the cost of each option is paid in lifecycle code, memory and how users experience playback leaving the screen. ## Option A: one player owned by the service The service builds one `ExoPlayer` and one `MediaSession` in `onCreate` and releases them in `onDestroy`. Screens connect a `MediaController` from a `SessionToken`; the controller implements `Player`, so `PlayerView`, Compose surfaces and the Material 3 `MiniController` composable (added in 1.11.0) work against it unchanged. What it buys: - **Playback outlives the screen**: the narration keeps going with the screen off, and the service runs in the foreground while playing. - **System integration for free**: the session feeds the media notification, lock screen, headset buttons, and other controllers such as a watch or a car. - **One decoder set and one owner**: a single instance means one place that calls `release()`, and no second screen accidentally holding another hardware decoder. - **Continuity across screens**: moving from the lesson list to a lesson detail does not rebuild the player or rebuffer. What it costs: - **Asynchronous connection**: `buildAsync()` returns a future, so a screen renders before it has a player and must handle that gap. - **Service lifecycle work**: the manifest's `mediaPlayback` type, the foreground grace period after a pause (10 minutes since 1.6.0), the `onTaskRemoved` policy, and playback resumption if you want it. - **Leaks move**: a screen that does not release its controller keeps the service bound, and the service cannot stop. - **Indirection**: commands pass through the session, which can filter them per controller, so debugging and tests involve a session and a controller rather than a plain player. ## Option B: a player per screen Each screen builds an `ExoPlayer` in `onStart`, attaches it and releases it in `onStop`. What it buys: - **Simplicity**: direct, synchronous calls, no service, no notification and no controller. - **Clear lifetime**: the screen's lifecycle is the player's lifecycle, which is easy to reason about and to test. - **Isolation**: a crash or misconfiguration in one screen's player does not affect another. What it costs: - **No background play** and no system controls, unless each screen adds its own session, which leads to duplicate sessions. - **Rebuffering on navigation**: moving between screens rebuilds the player and reloads media. - **Decoder pressure**: two screens alive at once, such as a list with a preview above a detail screen, can hold two decoders. ## Comparing them | Concern | Service-owned player | Player per screen | |---|---|---| | Screen off | Keeps playing | Stops | | System controls | Automatic through the session | None without extra sessions | | First frame on a new screen | Waits for the controller to connect | Immediate after build | | Release discipline | One owner; controllers must still be released | Every screen must release | | Testing | Needs a session and a controller | Plain player | ## A defensible team default For the language-learning app, split by **content type**, not by screen: 1. **Lessons**, whose audio must continue with the screen off: one service-owned player, one session, every screen as a controller. 2. **Muted previews** in a catalogue: short-lived local players with no session, released with the screen. How many previews to keep warm is a feed design decision, not this one. 3. **One session per app**: the `MediaSessionService` documentation recommends a single declared service and warns that multiple sessions make car and Bluetooth displays list the app several times. If a feature later needs a different player in the same session, `MediaSession.setPlayer` swaps it, provided the new player uses the same application looper. ## What to measure before committing - Time to first frame when navigating between the lesson list and a lesson, with each option. - How many players and decoders are alive during that transition. - How often learners lock the phone mid-lesson and keep listening; if almost nobody does, the service may not earn its cost. - Memory held while paused, since a service-owned player keeps its buffers for as long as the service keeps it, including the foreground grace period. ## How to enforce it - A lint or review rule: no `ExoPlayer.Builder` outside the service and the preview component. - A shared helper that connects and releases the controller with the screen's lifecycle. - A playback test that locks the screen mid-lesson and checks the narration continues.

  • What would make you choose a player per screen as the default instead?
    An app whose playback never outlives the screen: short muted clips, a video message preview, or a one-off tutorial with no background audio. Then the service, notification and controller add lifecycle cost with no user benefit, and a local player released in `onStop` is simpler and easier to test.
  • How does the lesson screen avoid a blank player while the MediaController connects?
    Treat the controller as state that arrives later: show a placeholder or the item's artwork until `buildAsync()` completes, then set the controller on the view. Because the service already holds the playlist and position, the screen shows the current lesson without rebuffering once connected.

  • Every screen should own its own MediaSession so each can be controlled separately.
  • A service-owned player removes all lifecycle work, because the service handles everything.
  • A MediaController cannot show video, so video screens always need their own ExoPlayer.
  • Per-screen players are always cheaper because the service keeps a player in memory.
  • MediaSession.setPlayer accepts any player, whatever thread it runs on.

Assumes Media3 1.11; the Material 3 MiniController composable arrived in 1.11.0 and the 10-minute foreground grace after a pause in 1.6.0.