APPNGHENHACANDROID
APPNGHENHACANDROID
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.