DEV Community

Android Notifications for controlling ExoPlayer

In this post, we will describe you how to play Exoplayer media in the background and control it through Android notifications.

Google is the developer of Exoplayer. YouTube and Google Play Movies used it previously, and it's open-source. This is an application-level media player for Android. It provides a replacement for Android's MediaPlayer API for playing audio and video.

Image description

In the screenshot above, you can see the stream is playing and you can control it from Android Notifications.

You can accomplish this by following these steps:

  1. Use Exoplayer PlayerNotificationManager instance. PlayerNotificationManager is the main class that encapsulates functionality such as starting, controlling, attaching, and untying the player from Android notifications.
PlayerNotificationManager.Builder playerNotificationManagerBuilder = new PlayerNotificationManager.Builder(this, ID, CHANNEL_ID);
playerNotificationManagerBuilder.setMediaDescriptionAdapter(mediaDescriptorInstance);
PlayerNotificationManager playerNotificationManager = playerNotificationManagerBuilder.build();
Enter fullscreen mode Exit fullscreen mode
  1. The next step is to use PlayerNotificationManager.MediaDescriptionAdapter to provide descriptive data for the current playing media item, including the media title, subtext, and thumbnail/icon.
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptorInstance = new PlayerNotificationManager.MediaDescriptionAdapter() {
    @Override
    public CharSequence getCurrentContentTitle(Player player) {
        return "<Media Main Title>";
    }

    @Nullable
    @Override
    public PendingIntent createCurrentContentIntent(Player player) {
        return null;
    }

    @Override
    public CharSequence getCurrentContentText(Player player) {
        return "<Media sub-Title>";
    }

    @Nullable
    @Override
    public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
    // Icon to display
        return bitmap;
    }
};

Enter fullscreen mode Exit fullscreen mode
  1. Attach the playerNotificationManager to the player by passing the player instance
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector);

playerNotificationManager.setPlayer(player)

Enter fullscreen mode Exit fullscreen mode
  1. Pass null to detach and free the player.
playerNotificationManager.setPlayer(null)

Enter fullscreen mode Exit fullscreen mode

PlayerNotificationManager is typically instantiated in the onCreate method of an activity or fragment. Like PlayerActivity


public class PlayerActivity extends AppCompatActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  PlayerNotificationManager.Builder playerNotificationManagerBuilder = new PlayerNotificationManager.Builder(this, ID, CHANNEL_ID);
playerNotificationManagerBuilder.setMediaDescriptionAdapter(mediaDescriptorInstance);
PlayerNotificationManager playerNotificationManager = playerNotificationManagerBuilder.build();
}
Enter fullscreen mode Exit fullscreen mode

When an application launches PlayerActivity, the Android status bar must show player notifications and player controls.

Exoplayer Controls are visible in Android status bar

Discussion (0)