Playback Notifications with ExoPlayer

Marc Bächinger
AndroidX Media3
Published in
2 min readMar 19, 2018

Displaying a notification with playback controls is a best practice for media apps on Android. For audio playback in the background it’s even a requirement to attach a foreground service to a notification. To help make this task easier, we’ve added a new PlayerNotificationManager component to ExoPlayer’s UI module (currently available in the dev-2 branch). Once attached to a player, the manager will post a notification and keep it in sync with the state and the timeline of the player.

This can be achieved in three steps:

  1. Create a PlayerNotificationManager instance.
  2. Create a PlayerNotificationManager.MediaDescriptionAdapter to provide descriptive data for the current playing media item.
  3. Attach the player (and detach when resources are freed).

Creating the PlayerNotificationManager

For video playback the PlayerNotificationManager is typically instantiated in the onCreate method of an activity or a fragment. Audio apps do the same when the service, which holds the player instance, is created:

The app passes the channel and notification id as well as the PlayerNotificationManager.MediaDescriptionAdapter to the constructor. The app implements the adapter to provide descriptive data about the current playing media item:

Attach and detach the player

Once the manager is created, the player needs to be attached when it’s instantiated:

Before the player is released, it should be detached from the notification manager, which causes the notification to be cancelled:

With these basic steps, a notification appears as soon as playback starts and is kept in sync with the state of the player.

Customization

There are various ways in which you may want to customize the notification to fit to the theme of your app. The manager allows you to select which playback control actions to use and provides setters for the notification properties. All of these have a (hopefully) sensible default value but can easily be changed to desired values.

Playback control actions

The notification shows standard playback control actions like play/pause, fast forward/rewind, next/previous and stop which can be omitted on demand:

Custom actions can be provided by an app by passing a CustomActionReceiver as the fifth parameter to the PlayerNotificationManager constructor:

A CustomActionReceiver can declare and handle additional actions placed after the playback actions.

Notification properties

The notification manager provides setters to customize the look and behavior of the notification. The setters correspond to properties of the NotificationCompat.Builder:

MediaSession

Finally, if your app integrates with the MediaSession API, for instance to support Google Assistant, you can set the token of the session to take full advantage of the media style notification:

We believe the PlayerNotificationManager makes it easy to post notifications for media playback with ExoPlayer. We are eager to hear your opinion and requirements to make it even more useful for ExoPlayer users. Let us know on the Github issue tracker.

--

--