Posted by Nazmul Idris, Android Devices and Media Developer Advocate
The Google Assistant is available across phones, speakers, Android TV and more.
And it can help users get more done throughout their day—where they need to add
something to a to-do list, turn on the lights, or play a song.
With music specifically, the Assistant has a lot to offer. It can understand
media commands across devices (“play rock music”) and supports media controls
(like pause, skip, fast forward, thumbs up). And users can also control Android
media playback through the Google Assistant. For example, you can pause playback
by telling the Google Assistant to “pause playback” without lifting a finger. Or
play music by saying “play rock on Universal Music Player”.
In order for the user to control playback in your Android Media app via the
Google Assistant, you can use the MediaSession
APIs to make this happen. We recommend that you use MediaSession over intents as
you craft your app’s integration with the Google Assistant.
How to use MediaSession in your app?
Your app must implement a MediaSession that handles a prescribed set of actions
as described in Interacting
with the Google Assistant.
Here are some sample projects that can help you get started using MediaSession
in your media apps:
Music Player sample
Integration with Leanback sample
To learn more about MediaSession here are some good resources:
with a MediaSession
Playback with the Google Assistant on Android TV
How does the Google Assistant use MediaSession?
The Google Assistant uses MediaSession in the same way as other external
controllers such as Android Wear do. Each of these controllers cause state
changes inside your app, and there needs to be a way to respond to these
changes. This is where MediaSession comes into play.
By using MediaSession, the Google Assistant can control your app’s media
playback, as well as stay synced with its current state.
How do I test my app with MediaSession?
Once you implement MediaSession you will probably want to test how playback can
be controlled outside of your app. The Media
Controller Test tool was created to emulate external control of your media
playback. You can verify that when other apps (like Google Assistant) interact
with your app, that it works as expected.
Android Developers Blog