Restrict media routing for MediaStyle notification

123 views Asked by At

When I create a MediaStyle notification, it exposes a button to choose media routing, like this one: see screenshot.

How do I get rid of this button? Or at least, how can I restrict playback to only local device (phone speaker)?

Here's how I create the notification:

    MediaSessionCompat mediaSessionCompat = new MediaSessionCompat
            (this, TAG, mComponent, mediaButtonReceiverPendingIntent);
    mediaSessionCompat.setMetadata
            (new MediaMetadataCompat.Builder()
                    .putString(MediaMetadata.METADATA_KEY_TITLE, title)
                    .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
                    .build()
            );
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder
            (this, CHANNEL_ID);
    notificationBuilder
            .setContentTitle(title)
            .setStyle(new MediaStyle()
                    .setMediaSession(mediaSessionCompat.getSessionToken())
                    .setShowActionsInCompactView(0, 1, 2));
1

There are 1 answers

0
RaNo On BEST ANSWER

Okay, I think I found the solution. Here's the code:

private VolumeProviderCompat getVolumeProvider() {
    final int VOLUME_UP = 1;
    final int VOLUME_DOWN = -1;
    return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, 1, 0) {
        @Override
        public void onAdjustVolume(int direction) {
            if (direction == VOLUME_UP) {
                plugin.sendNext();
            }
            else if (direction == VOLUME_DOWN) {
                plugin.sendPrevious();
            }
        }
    };
}

And then use it like this:

mediaSessionCompat.setPlaybackToRemote(getVolumeProvider());