These guides discuss the MediaCompat APIs, which are no longer updated. We strongly recommend using the
Jetpack Media3 library instead.
Media session callbacks
Stay organized with collections
Save and categorize content based on your preferences.
Since a video app runs its media session and media controller in the same activity, the media session callbacks
are different from the implementation shown for the
audio app
server/client architecture. There are no service calls, and notifications are handled via the NotificationManager. The following table shows how the various features are controlled in each callback method:
onPlay()
onPause()
onStop()
Audio Focus
requestFocus()
passing in your OnAudioFocusChangeListener
.Always call requestFocus()
first, proceed only if focus is granted.
abandonAudioFocus()
Media Session
setActive(true)
- Update metadata and state
- Update metadata and state
setActive(false)
- Update metadata and state
Player Implementation
Start the player
Pause the player
Stop the player
Becoming Noisy
Register your BroadcastReceiver
Unregister your BroadcastReceiver
Notifications
Show notification
Update notification
Note: People using the Google Assistant can control your app with voice commands
if you create your MediaSession with the necessary callbacks. The
requirements are explained in the
Google Assistant documentation .
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License , and code samples are licensed under the Apache 2.0 License . For details, see the Google Developers Site Policies . Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-07-24 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]