Android MediaSession

With Android 5.0 “Lollipop”, Google introduced the MediaSession API to simplify control of media on Android.  I’ve been building a podcast app (PremoFM) for the better part of a year, so I have some intimate knowledge of MediaSession, so I told the world about it at a TriDroid Meetup.  My learnings with the MediaSession API, it makes the following significantly easier:

  • Controlling playback, in the case where your MediaPlayer is in a background service and you present the user with controls in an activity, MediaController
  • Broadcasting state and playback metadata changes
  • Android Wear media playback control
  • Bluetooth playback control

Trust me, use the API.  I spent the better part of the last year working against the framework, until I did a deep dive and finally began to understand the purpose.  Anyone, my learnings are in a Google Slides slide deck:

-> Playing Media on Android 5 & Beyond

I’ve posted a very rough sample Android project on GitHub with some crude examples of using MediaSession to control playback in a service, from a notification and activity.

-> MarshmallowFM on GitHub

Here is a great explainer video about MediaSession.