[MUSIC PLAYING]
PAUL: There's a hip way to play media on Android.
Whether you've got viewers or listeners,
that are modern ways for them to engage with your content.
Hi, I'm Paul [INAUDIBLE],, and in this video,
we'll be exploring modern media playback on Android.
Media apps enjoy many surfaces on Android
from phones to tablets, smart TVs to smartwatches,
and everything in between.
And the type of media can be equally diverse, movies,
TV series, video clips, workout sessions, podcasts,
or just plain old music.
But they have one thing in common.
The user is in control.
So let's take a look at the different ways in which users
can control media.
The Android platform itself offers a few surfaces
through which media can be controlled
from outside your app, in particular,
through the dedicated media controls
near the quick settings on phones and tablets
and in the media overlay on Wear OS.
Some devices, such as TVs through remote controls
or Chromebooks with peripherals, allow
controlling media playback through dedicated buttons.
Of course, media can also be controlled through accessories
like wired headphones and Bluetooth speakers or earbuds.
And finally, let's not forget that users
may be controlling media by voice through the Google
Assistant.
Your media app likely consists of two main parts, first,
a media player, which renders audio and video content,
and the user interface which shows
what's playing and provides a way for users
to control playback.
However, in this architecture, the Android system
and other apps running on the device
aren't aware that media is being played.
And so they can't provide a convenient way
for your user to view and control the playback.
And this is where the media session API comes in.
Media sessions provide a universal way
of interacting with a audio or video player.
By informing Android that media is playing an in an app,
playback controls can be delegated into that app.
Integrating with the media session
allows an app to advertise media playback externally
and to receive playback commands from external sources.
The media session can then delegate these commands
to the app where it can apply them to control
its internal media state.
By connecting your player to a media session,
you can expose information about what
you're playing to the system and enable those mechanisms
we talked about earlier for controlling playback
from outside your app.
There are four key components that your app should
provide into the media session.
First, the playback position and playback state
are essential for the platform to reflect
what the current status of the media is.
Next, informing the media session of which actions
are supported brings those controls
onto the various surfaces for the user to interact with.
The content metadata is critical when informing
the user what is playing.
And finally, queue items allow the media sessions
to know what's up next and can take
care of showing previous and next controls where applicable.
While it's easy to visualize how this comes together
in Android's media controls, also
consider the voice and peripheral interactions
we looked at earlier.
Now let's look at ways in which the media session provides
actions back into your app.
Media session will only serve as actions to users
that you've declared to support.
Those actions may be basic playback controls like play
and pause, stop, or skipping to the previous and next piece
of content.
In order to support seek controls,
it's essential to provide accurate playback position
and state.
And to get that nailed, you can even
specify the playback speed.
Custom actions can also be specified, for instance,
to allow you to implement toggling captions or setting
ratings.
There are some great resources on how to integrate
your app with media sessions.
If you're using ExoPlayer player,
check out what's next for AndroidX Media and ExoPlayer
by Dawn Turner and Andrew Lewis from the Android media team.
You can also find an in-depth guide
on media session on developer.android.com.
And the links to these resources and more
can be found in the video description below.
Once your implementation is complete,
there are a few techniques for validating
that things work correctly.
Let's first take a look at some of the automated validations.
The media controller test app allows
you to test the intricacies of media playback on Android
and helps verify your media session implementation.
Its verification testing framework
offers one click tests that you can
run to ensure that your media app responds correctly
to a playback request, which works both on mobile devices
and on TV.
Media session validator is a tool
that provides an easy and automated way
to verify your media session integration on Android TV.
As an added benefit, It also verifies
a prerequisites of supporting cast connect in your TV app.
If you prefer to manually validate the media session
implementation, for instance, for integrating tests,
you can inspect its state as different media actions
are issued.
Let's take a look.
To dump the state of the device's media session,
run ADB shell dumpsys media_session.
In its output, we're looking for a few things.
First, find the media session with a tag that you defined,
which should correspond to your application's package name.
Inside the details, look for some key pieces of information.
The state of active should reflect when media should
be able to be controlled.
Also validate that the media session
corresponds to what is playing.
Finally, let's parse the contents
of the integer for actions.
We can confirm which actions are present from the bit mask
encoded by the actions value.
The values of these actions can be found in the playback state
reference.
A set of media commands can be issued through ADB shell media
dispatch, including play, pause, rewinding, and so on.
Note that the more complex actions, such as seeking,
rating content, or performing custom actions cannot be
invoked in this manner.
Let's review a checklist for building a great media
experience on Android.
First, make sure only a single media session
is being used in your app.
Check the places where your media session is created,
and validate that there's no pathway for duplicate ones
to be created in parallel.
Critically, mark the media session is active
and inactive when necessary.
If you don't support media playback in the background,
this will generally be in your activity, your fragments
on stop and on start methods.
If your app plays music in the background,
the media session should be marked as active
when the service starts and inactive
when the service is finished.
Third, be sure to always be setting and updating
media playback state.
When you first create the media session,
set the playback state.
When the state changes, for instance,
because it transitions from playing to paused, update it.
You do not need to periodically update the playback state just
from regular playback like updating position
every second during playback.
Android will calculate the new position from the playback
rate, so you only need to provide updates to the playback
state when it actually changes.
You should also ensure to update any playback
actions that you support.
Getting this right is difficult, but it's
made much easier with AndroidX Media3
because when used in conjunction with ExoPlayer
the player takes care of this for you.
For more information on Media3, click
the What's Next for AndroidX Media and Exoplayer
tip up here.
Fourth, set and update the media metadata.
When you first create the media session,
set the media metadata.
Update it when the media metadata changes, for instance,
because the next song or episode has started.
Using media items in Jetpack Media3 in conjunction
with Exoplayer largely handles these cases for you.
An important property of the metadata is the duration.
Without it, the media style notification
does not show the seek bar on recent API levels.
Fifth, verify handling of the core commands.
Ensure that the app handles play, pause,
stop, and seek incorrectly.
But also ensure that the app handles
the play next in previous commands
if your content is sequential like a playlist or a TV series.
Sixth, I recommend performing a full validation
using the techniques we covered earlier.
And finally, release the media session when necessary.
It's most sensible to do this when the media
player is released as well.
In closing, there are a few common pitfalls
that we've seen in media session implementations.
Some developers sometimes choose to handle raw KeyEvents
instead of handling media actions from MediaSession.
This inhibits you from expanding your app
to support more delightful interactions, such as seeking,
submitting ratings, or handling custom actions.
A frequent mistake for video apps
is to keep the media session active when
the user background's your app.
If your media doesn't support background playback,
be sure to set is active to false on its media session.
Finally, be wary of ways in which lifecycle changes may
affect the way actions are handled.
Consider, for instance, that invoking Google Assistant
may cause your app to lose focus when a user requests
the media to pause.
Consider in that example if your app correctly
pauses the media when the focus is returned.
I hope that this overview helps you
build excellent media experiences
on Android that lets users interact
with your content in novel ways.
To learn more, check out our documentation
and other resources linked in the video description below.
And while you're checking out that video description
down there, why not hit the like and subscribe.
[MUSIC PLAYING]