Media Playback and the must-have Support Library APIs

When it comes to Best practices in media playback, there are a lot of APIs to work with to build the best experience. Thanks to the Support Library, the one thing you won’t need to worry about is API level. But there are a lot of Support Libraries, and with the v4 Support Library split in version 24.2.0, finding all the media APIs you might need still requires a bit of thought.

What you really need to build a media playback app

Media-Compat Support Library

One of the first thing you might notice looking over the new split v4 Support Library is a shiny new Media-Compat Support Library. As you might expect, this is where the vast majority of the media-specific Support Library functionality lives.

It all starts with MediaSessionCompat, which is squarely at the center of integrating your media app with the system. This is how you tell the system what media is playing by providing metadata, such as the title, artist, and album, and what media button events that your app supports, such as play or pause.

To aid in handling those media button events, the Support Library only MediaButtonReceiver makes it easy to get media button events from the BroadcastReceiver (required on pre-API 21 devices) to your media playback Service where you can handle them appropriately. Best of all: it is a single manifest entry and a single line of code.

Beyond that, the MediaBrowserServiceCompat and MediaBrowserCompat classes serve as the backbone for a modern media playback app. They serve as a pre-built communication protocol between your media playback Service and other components such as your UI or other apps such as Android Auto.

Fragment Support Library

Media-Compat also contains two static convenience methods, MediaControllerCompat.setMediaController() and MediaControllerCompat.getMediaController(), that require that you use FragmentActivity, part of the Fragment Support Library if you want to support devices prior to API 21.

Besides giving you an easy way to retrieve your MediaControllerCompat to trigger actions from your UI, they also pass through the call to the API 21+ setMediaController() that captures media buttons and volume events while your Activity is active and ensures they get sent to your MediaController (even if your MediaSession is inactive such as in cases where playback has yet to start).

AppCompat

The AppCompat Support Library backports much of the material design theming that is expected in apps these days, but it also is the home of NotificationCompat.MediaStyle.

MediaStyle is a style of notification added in API 21 that, through the Support Library is available on older devices as well. Since controlling playback is such a critical piece to media playback apps, MediaStyle makes these a priority, making the actions you add to your notification available either in the collapsed notification or even more actions in the expanded notification.

MediaRouter Support Library

Critical to Google Cast support among others, the MediaRouter Support Library is what builds out the connect button, dialog, and other components needed for interacting with these devices.

If you’re using Cast v3 for Android, including the MediaRouter Support Library is one of the things you’ll do right from the start.

Every Support Library?

Of course, there’s a good reason that each of the many Support Libraries exist — they have useful functionality that can simplify the logic within your app or add whole new functionality decoupled from Android API level. While the libraries outlined above are all of the ones needed to support the media APIs you need to build a great app, I’d encourage you to look closely at the full list of classes throughout the android.support namespace when building your app.

#BuildBetterApps