10/10/17

The Google Assistant can help control your Android media apps

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 href="https://medium.com/google-developers/understanding-mediasession-part-1-3-e4d2725f18e4">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 href="https://developer.android.com/guide/topics/media-apps/interacting-with-assistant.html#use_a_media_session">Interacting
with the Google Assistant.



Here are some sample projects that can help you get started using MediaSession
in your media apps:


  1. href="https://github.com/googlesamples/android-MediaBrowserService/">MediaBrowserService
    sample
  2. href="https://github.com/googlesamples/android-UniversalMusicPlayer">Universal
    Music Player sample
  3. Assistant
    Integration with Leanback sample


To learn more about MediaSession here are some good resources:


  1. href="https://medium.com/google-developers/understanding-mediasession-part-1-3-e4d2725f18e4">Understanding
    MediaSession
  2. href="https://developer.android.com/guide/topics/media-apps/working-with-a-media-session.html">Working
    with a MediaSession
  3. href="https://android-developers.googleblog.com/2017/10/video-playback-with-google-assistant-on.html">Video
    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 href="https://github.com/googlesamples/android-media-controller">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.





Load disqus comments

0 comments

loading...