Android Notifications for controlling ExoPlayer
Tags: exoplayer, android, java, tutorial
Published: 2022-02-22 17:44:39

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivh6nnyhn3wn7swpnc05.png)


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](https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ui/PlayerNotificationManager.html) instance. PlayerNotificationManager is the main class that encapsulates functionality such as starting, controlling, attaching, and untying the player from Android notifications.

```java
PlayerNotificationManager.Builder playerNotificationManagerBuilder = new PlayerNotificationManager.Builder(this, ID, CHANNEL_ID);
playerNotificationManagerBuilder.setMediaDescriptionAdapter(mediaDescriptorInstance);
PlayerNotificationManager playerNotificationManager = playerNotificationManagerBuilder.build();
```



2. 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.


```java 
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;
    }
};

```
 
3. Attach the *playerNotificationManager* to the player by passing the player instance

```java
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector);

playerNotificationManager.setPlayer(player)

```
4. Pass null to detach and free the player. 


```java
playerNotificationManager.setPlayer(null)

```


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

```java

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();
}
```


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

![Exoplayer Controls are visible in Android status bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exnevsgr6av7we59c6bh.png)