Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the auto generated media3 notification ? (java)

I build an exoplayer with Media3 1.0.0-rc01 with a mediasession service, like describe in developer guide

It's generated a media notification automatically and i want to change colors (text, background) and icon (and more).

The guide says : "To provide a custom notification, create a MediaNotification.Provider with DefaultMediaNotificationProvider.Builder or by creating a custom implementation of the provider interface. Add your provider to your MediaSession with setMediaNotificationProvider."

So i created a new class for the provider :

package myapp;

import android.content.Context;
import android.os.Bundle;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.session.CommandButton;
import androidx.media3.session.DefaultMediaNotificationProvider;
import androidx.media3.session.MediaNotification;
import androidx.media3.session.MediaSession;
import com.google.common.collect.ImmutableList;

@UnstableApi
public class MyMediaNotificationProvider implements MediaNotification.Provider {

    private Context context;
    public MyMediaNotificationProvider(Context context) {
        this.context = context;
    }

    @Override
    public MediaNotification createNotification(
            MediaSession mediaSession,
            ImmutableList<CommandButton> customLayout,
            MediaNotification.ActionFactory actionFactory,
            Callback onNotificationChangedCallback) {

        // Create the notification
        return new DefaultMediaNotificationProvider(context).createNotification(
                mediaSession,
                customLayout,
                actionFactory,
                onNotificationChangedCallback);
    }

    @Override
    public boolean handleCustomCommand(
            MediaSession session,
            String action,
            Bundle extras) {
        return false;
    }

}

And I added the setMediaNotificationProvider(new MyMediaNotificationProvider()); in my PlaybackService class, to call the new created class above :

package myapp;

import android.net.Uri;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.session.MediaSession;
import androidx.media3.session.MediaSessionService;

@UnstableApi public class PlaybackService extends MediaSessionService {
    private MediaSession mediaSession = null;

    @Override
    public MediaSession onGetSession(MediaSession.ControllerInfo controllerInfo) {
        // If desired, validate the controller before returning the media session
        return mediaSession;
    }

    // Create your Player and MediaSession in the onCreate lifecycle event
    @Override
    public void onCreate() {
        super.onCreate();

        // Create the player and the mediasession
        ExoPlayer player = new ExoPlayer.Builder(this).build();
        mediaSession = new MediaSession.Builder(this, player).build();

        // Build the media item.
        Uri mp3 = Uri.parse("android.resource://myapp/raw/mysoundtoplay");
        MediaItem mediaItem = MediaItem.fromUri(mp3);
        // Set the media item to be played.
        player.setMediaItem(mediaItem);
        // loop mode
        player.setRepeatMode(player.REPEAT_MODE_ALL);
        // Prepare the player.
        player.prepare();
        // Play
        player.setPlayWhenReady(true);

        // CALL MY NEW CLASS TO CREATE NOTIFICATION
        Context context = getApplicationContext(); // context must be passed to class
        setMediaNotificationProvider(new MyMediaNotificationProvider(context));

    }

    // Remember to release the player and media session in onDestroy
    @Override
    public void onDestroy() {
        mediaSession.getPlayer().release();
        mediaSession.release();
        mediaSession = null;
        super.onDestroy();
    }
}

The app is doing fine (sound is playing) and the notification is visible

The androidx migration guide says "An app can customize the notification by setting a custom MediaNotification.Provider in onCreate() that replaces the DefaultMediaNotificationProvider. The MediaLibraryService then takes care of starting the service in the foreground as required. By overriding MediaLibraryService.updateNotification() an app can further take full ownership of posting a notification and starting/stopping the service in the foreground as required."

Anyone can help me customizing the notification (text, color, icon...) ?

Thanks

like image 509
Pepsi_34 Avatar asked Oct 11 '25 11:10

Pepsi_34


1 Answers

First thing, media3 v1.1.1 was made available August 16, 2023.

To set the notification icon, adjust your DefaultMediaNotificationProvider in MyMediaNotificationProvider#createNotification() to the following:

            // Create the notification
            DefaultMediaNotificationProvider defaultMediaNotificationProvider = new DefaultMediaNotificationProvider(mContext);
            defaultMediaNotificationProvider.setSmallIcon(R.drawable.blb_reverse_icon);
            MediaNotification mediaNotification = defaultMediaNotificationProvider
                    .createNotification(
                            mediaSession,
                            customLayout,
                            actionFactory,
                            onNotificationChangedCallback);

            return mediaNotification;

To set titles and background, you will need to set metadata on your MediaItem(s) using setMediaMetadata().

    Uri artworkUri = Uri.parse("android.resource://"+mActivity.getPackageName()+"/"+R.drawable.mediasession_bg_artwork);
    Uri mp3 = Uri.parse("android.resource://"+mActivity.getPackageName()+"/"+R.raw.myMedia.mp3);
    MediaItem mediaItem = new MediaItem.Builder()
            .setMediaMetadata(new MediaMetadata.Builder()
                    .setTitle("My Title")
                    .setArtist("My Artist")
                    .setArtworkUri(artworkUri)
                    .build()
            )
            .setUri(mp3)
            .build();

You can find more information on MediaItem metadata Media3 MediaMetadata.Builder

The trick for the background is in the .setArtworkUri().

like image 200
Dan Davis Avatar answered Oct 14 '25 02:10

Dan Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!