Understanding MediaSession (Part 1/4)

Is MediaSession for me?

As an Android developer working with Media APIs, you might have heard about MediaSession. You might be curious about what it does, when to use it, and when not to.

Before going into the details you can use this flowchart to determine if your media playback app will greatly benefit from using it.

The goal of this series of articles is to give you a deep understanding of MediaSession, what it is good for, when to use it, and when not to. This is the first part of a 4 part series that includes:

  1. Is MediaSession for me? (this article)
  2. Making sense of the complex media landscape
  3. How to use it for simple use cases
  4. How to use it for complex use cases

Examples of media playback apps

Let’s go through a few types of apps you might be building to see if MediaSession is needed.

Video game app

Video games often include music and sound effects to enhance the user’s experience, but, in this case, the user usually doesn’t have any expectation of being able to pause the music or skip to the next track, so there would be no need to use a MediaSession here.

Video player app

A video player may only play audio and video while the app is in the foreground, so it may not seem necessary to integrate with MediaSession. However, in order for your app to respond to media button events you would need to integrate with MediaSession.

Music player app

A music player app should absolutely integrate MediaSession. MediaSession provides hooks for playback control via media buttons on headphones, Android Wear, Android Auto, and Google Assistant. It might also benefit from utilizing a MediaBrowserService. By providing a MediaBrowserService, the app can allow users to use Android Auto, Wear, Assistant, and Bluetooth devices to navigate the media contents (without using the app’s UI).

To learn more about what MediaSession is and isn’t and the larger context in which it fits in the Android ecosystem read the second article in this this series.

Android Media Resources

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.