Two players communicating using 2 media players - devrath/MediaAlchemySuite GitHub Wiki

🎬 Your App: Playing Video with Media3 (ExoPlayer 3)

  • ExoPlayer: Plays the video
  • MediaSession: Informs the system that your app is playing media
  • MediaSessionService: Runs in the background, holds the session and player
  • MediaController: Used by system UI or your app to send playback commands

🎧 Another App (e.g., YouTube) Starts Playing

  • Android has a global MediaSessionManager that tracks all media sessions
  • Only one active session at a time (gets system focus)

What Happens:

  1. YouTube starts playing → Android focuses on its session

  2. Your MediaSession goes to background or pauses

  3. You receive onAudioFocusChange(...)

    • Your MediaSessionService should pause the player
  4. If user comes back to your app:

    • You request audio focus again
    • If granted, your MediaSession becomes active
    • System routes media buttons, lock screen controls, and notifications to your app again

🔧 Visual Logic

Your App          YouTube App
[Session]         [Session]
    ↓                 ↓
   MediaSessionManager (Android)
         ↓
 Routes play/pause to only one session

🧩 MediaLibraryService

  • Needed only if client apps (e.g., Android Auto, Google Assistant) want to browse your media
  • It exposes a media catalog
  • Not required for simple video playback

✅ Summary

  • You create MediaSession in a MediaSessionService tied to ExoPlayer
  • Android decides which session is active based on audio focus
  • Only one session can handle system playback controls
  • Another app starting media will likely pause yours
  • Use MediaLibraryService only for exposing a browsable media library to external clients