Android Media3 & ExoPlayer 🎬
Handling streaming audio, video playback, and Picture-in-Picture.
⚡ When to Use
- When asked to build video players, music streaming services, or complex media apps.
🏗️ Core Rules / Pillars
1. Migrating to Media3
- Pattern:
com.google.android.exoplayer2is fully deprecated. You MUST useandroidx.media3:media3-exoplayer. - Implementation: Instantiate the
ExoPlayer.Builder(context).build()and strictly manage its lifecycle, hooking it to Compose'sDisposableEffectto triggerplayer.release()when leaving the screen.
2. Audio Focus & Background Playback
- Pattern: If building a music app, you MUST use
MediaSessionconnected to a Foreground Service. - Implementation: Request Audio Focus automatically using Media3's
player.setAudioAttributes(..., handleAudioFocus = true)so the player pauses gracefully when a phone call comes in.
🚧 Critical Anti-Hallucination Guards
- Trap: Do not hallucinate
MediaBrowserServiceCompat. Media3 replaces the entire legacy MediaSessionCompat stack with a unifiedMediaSessionService.