Modern Android applications that output audio or video—such as streaming players, podcast clients, and games—integrate with the Android framework through the MediaSession architecture. A media session allows applications to expose playback state, track metadata, queue information, and transport controls to external listeners like lock screens, notification shade widgets, Wear OS smartwatches, and Android Auto dashboards. While developers often interact with media sessions programmatically, testing session callbacks, auditing token registrations, and simulating playback hardware keys from the command line is streamlined using the Android Debug Bridge (ADB) media_session service manager.
Understanding Android MediaSession Architecture
The Android media framework enforces a strict separation between playback UI and playback logic. The player daemon registers an instance of MediaSessionCompat (or modern MediaSession) with the operating system’s MediaSessionService. In turn, external consumers interact with that session using a MediaController.
When an application gains audio focus and plays media, it registers its session token as active. The operating system uses this registry to decide which app responds when a user presses the play/pause button on a Bluetooth headset or steers media from the quick settings panel. The ADB cmd media_session command provides direct shell-level access to this internal routing engine, enabling engineers to inspect registered tokens, monitor volume changes, and dispatch media button events without touching the physical device screen.
Verifying ADB Connection and Accessing the Media Session Service
Ensure that your Android device is connected to your development workstation with USB Debugging enabled. Verify your connection by opening a terminal and running:
adb devices
Once your device serial appears in the list, access the help overview for the media session command service:
adb shell cmd media_session help
This displays the supported sub-commands, including transport dispatchers, session dump utilities, and key-event injectors.
Listing and Auditing All Active Media Sessions
To inspect which applications are currently holding active media sessions or registered tokens on the device, execute the dump command:
adb shell dumpsys media_session
The resulting output delivers an exhaustive technical breakdown of every session registered with the framework. To isolate the summary of active sessions, search specifically for the sessions section:
adb shell dumpsys media_session | grep -A 20 "Sessions Stack"
The output presents details structured similarly to the following:
Sessions Stack - have 2 sessions:
1: MediaSessionRecord{ownerPackage=com.spotify.music, tag=SpotifyMediaSession, pid=14520}
state=PlaybackState {state=3, position=42150, buffered position=90000, speed=1.0, updated=125430}
metadata=MediaMetadata: size=5, description=Track Title, Artist Name
active=true
flags=3
2: MediaSessionRecord{ownerPackage=com.google.android.youtube, tag=YouTubeVideoSession, pid=18940}
state=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=110200}
active=false
This report exposes essential operational metrics:
- ownerPackage: The specific application package managing the session.
- state: The current playback state integer (where
state=3representsSTATE_PLAYING,state=2representsSTATE_PAUSED, andstate=1representsSTATE_STOPPED). - position: The current playback position in milliseconds.
- active: Indicates whether the session is currently eligible to receive media key broadcast events.
Dispatching Media Control Commands via ADB
The cmd media_session dispatch sub-command allows you to send standardized playback actions directly to the top active media session without simulating coordinate touches on the touch screen.
To toggle playback (play/pause) on the active player, execute:
adb shell cmd media_session dispatch play-pause
You can also control transport navigation using dedicated action arguments:
- Pause playback:
adb shell cmd media_session dispatch pause - Resume playback:
adb shell cmd media_session dispatch play - Skip to next track:
adb shell cmd media_session dispatch next - Return to previous track:
adb shell cmd media_session dispatch previous - Fast-forward:
adb shell cmd media_session dispatch fast-forward - Rewind:
adb shell cmd media_session dispatch rewind - Stop playback completely:
adb shell cmd media_session dispatch stop
These commands bypass the graphical user interface entirely and interact directly with the app’s MediaSession.Callback methods, making them ideal for automated UI and audio testing pipelines.
Simulating Hardware Media Button Key Events
In addition to direct semantic session dispatches, Android devices receive playback triggers from physical buttons on wired earphones, Bluetooth headsets, and steering wheel interfaces. These triggers arrive as low-level Linux key codes via the Android input subsystem.
To test how your media app handles raw media button keypresses, use adb shell input keyevent:
# Send Play/Pause toggle key event (KEYCODE_MEDIA_PLAY_PAUSE = 85)
adb shell input keyevent 85
# Send Next Track key event (KEYCODE_MEDIA_NEXT = 87)
adb shell input keyevent 87
# Send Previous Track key event (KEYCODE_MEDIA_PREVIOUS = 88)
adb shell input keyevent 88
Dispatching these events verifies that your application’s MediaButtonReceiver correctly handles headset button hooks and hardware accessory events.
Auditing Media Volume and Routing Policies
When multiple media players or system streams compete for audio output, the operating system applies strict volume scaling and audio attribute policies. To inspect current volume levels and audio stream routing associated with active media sessions, run:
adb shell dumpsys audio | grep -A 10 "STREAM_MUSIC"
This displays the current volume index, mute states, and output device routing (such as built-in speaker, wired headphones, or Bluetooth A2DP), allowing you to ensure your media session obeys volume normalization standards across diverse output peripherals.
Utilizing cmd media_session provides developers and QA teams with full programmatic mastery over Android media playback, accelerating background audio validation and accessory integration.