0% found this document useful (0 votes)
19 views

APPNGHENHACANDROID

MediaBrowserServiceCompat and MediaBrowserCompat serve as a communication protocol between media playback services and other app components. MediaSession acts as middleware allowing external actors to control media playback and the app to report state changes, while services manage media and clients connect to browse content and control playback.

Uploaded by

Hoang Tung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

APPNGHENHACANDROID

MediaBrowserServiceCompat and MediaBrowserCompat serve as a communication protocol between media playback services and other app components. MediaSession acts as middleware allowing external actors to control media playback and the app to report state changes, while services manage media and clients connect to browse content and control playback.

Uploaded by

Hoang Tung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MediaBrowserServiceCompat and MediaBrowserCompat serve as a pre-built communication protocol

between your media playback Service and other components.

MediaSession does the following things:

 Playback Control — It provides a single interface to control playback (eg: play, pause,
stop, skip to next, play for search) that can be used by your app’s UI and external actors
(eg: media buttons, Google Assistant). This might seem confusing at first, but if you
bypass the MediaSession and directly control your player, then your app’s UI can easily
go out of sync with the player and session when outside actors change playback state, or
load different media.

 State Sync — It broadcasts the current playback state (playing, paused, stopped, etc) and
media metadata (album art, song duration, song title, etc) to all outside actors, and your
app itself.

MediaSession is the middleware that allows outside actors to control the media player in your app
(outside of the app’s own UI). It also allows your app to report changes in its player’s state (what media
is loaded, is the player currently paused, or playing, etc) to interested parties outside and inside of your
app.

 Pressing on the media pause button fires an Intent that is received by your app. The
MediaSession then transmits this to your app by calling onPause() in the callback to your code
that you provide to the MediaSession. A very similar thing happens when the Google Assistant
is asked to “pause playback”. Both paths end up in the onPause() method that is called by the
MediaSession into your code.
 At this point, you are responsible for implementing onPause() in your music service and
connecting to your player and actually pausing its playback. As soon as this is done, you are
responsible for reporting that the playback state has changed, by communicating this back to the
MediaSession by calling setPlaybackState() with the new state.
 Your app’s UI should connect to the MediaSession and when this playback state is changed,
you have to implement an onPlaybackStateChanged() method in your UI code. Here, you can
update your UI to show that the playback state has changed to pause

 Service — Create a Service that manages the player and handles preparing and playing
media. Create MediaStyle notifications that are tied to this service. This service needs to
extend MediaBrowserService in order to provide content (eg: this is how Android Auto
can browse the content provided by the app). The details for the service are provided in
the sections below.

 Client — Create an Activity or Fragment that connects to this service using


MediaBrowser. MediaBrowser allows access to the content provided by the Service,
and allows the use of MediaSession to control playback and get updates on what media
is loaded and the playback state changes (which actually occur in your Service). The
details for the client are provided in the sections below.

You might also like