MDT
MDT

Reputation: 1695

Disable or hide seekbar in MediaStyle notifications

Trying to build a live stream media playback app, the media style notifications in 28 and below sdk looks good without any seekbar, but when running same application in Android 10 (SDK 29) the notification is showing additional seekbar which i don't want since the stream is live and i am using default exoplayer (exo vers. 2.10.8) behavior to cache.

How do i disable or hide the seekbar?

tried setting below in notification builder:

.setProgress(0,0,true)

Snippet of notification below :

    Notification notification = new Notification.Builder(this,Constant.CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle(title)
            .setContentText(message)
            .setLargeIcon(artwork)
            .addAction(new Notification.Action.Builder(
                    Icon.createWithResource(getApplicationContext(),playPauseResourceId),
                    "Play/Pause",
                    playPausePendingIntent).build())
            .addAction(new Notification.Action.Builder(
                    Icon.createWithResource(getApplicationContext(),R.drawable.exo_icon_stop),
                    "Play/Pause",
                    stopPendingIntent).build())
            .setStyle(new Notification.MediaStyle().setShowActionsInCompactView(0).setMediaSession(mediaSession.getSessionToken()))
            .setSubText(subText)
            .setContentIntent(pendingActivityIntent)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setProgress(0,0,true)
            .build();

Screeshot :

enter image description here

Upvotes: 10

Views: 6821

Answers (4)

Kurdev
Kurdev

Reputation: 49

Try to remove

    .setMediaSessionToken(mediaSession.getSessionToken());

in fact that let u show the seek bar on notification .

Upvotes: 1

GreatC
GreatC

Reputation: 400

I also encountered this problem, but I am using NotificationCompat instead of exoplayer.
I followed Squti's answer and found the solution to hide the seek bar for NotificationCompat.

val mediaSession = MediaSessionCompat(context, "your tag")

//These two lines work
val mediaMetadata = MediaMetadata.Builder().putLong(MediaMetadata.METADATA_KEY_DURATION, -1L).build()
mediaSession.setMetadata(MediaMetadataCompat.fromMediaMetadata(mediaMetadata))

val token = mediaSession.sessionToken

val mBuilder = NotificationCompat.Builder(context, channelId)
    .setStyle(androidx.media.app.NotificationCompat.MediaStyle()
            .setMediaSession(token))

Upvotes: 9

MDT
MDT

Reputation: 1695

One other way apart from what Squti has provided as a solution.

Just don't set MediaSession token in you media style notification. So as per above snippet posted by me in the query the notification would be defined like below :

Notification notification = new Notification.Builder(this,Constant.CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle(title)
            .setContentText(message)
            .setLargeIcon(artwork)
            .addAction(new Notification.Action.Builder(
                    Icon.createWithResource(getApplicationContext(),playPauseResourceId),
                    "Play/Pause",
                    playPausePendingIntent).build())
            .addAction(new Notification.Action.Builder(
                    Icon.createWithResource(getApplicationContext(),R.drawable.exo_icon_stop),
                    "Play/Pause",
                    stopPendingIntent).build())
            .setStyle(new Notification.MediaStyle().setShowActionsInCompactView(0))
            .setSubText(subText)
            .setContentIntent(pendingActivityIntent)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .build();

Just remove .setMediaSession(mediaSession.getSessionToken()) from below :

.setStyle(new Notification.MediaStyle().setShowActionsInCompactView(0).setMediaSession(mediaSession.getSessionToken()))

Not using or assigning the token to notification would mean that you wont have the controls you need to control your media service, also you loose feature like automatic setting of color(color temperature auto detected from album-art) on your notification and would need to write custom indents for controlling the player.

Upvotes: 6

Squti
Squti

Reputation: 4487

You need to use PlayerNotificationManager instead of Notification.Builder and pass custom Bundle extra to MediaDescriptionCompat.Builder using setExtras() method with MediaMetadataCompat.METADATA_KEY_DURATION key and -1 value then override getMediaDescription() method of TimelineQueueNavigator class and pass it to MediaSessionConnector like so :

mediaSessionConnector.setQueueNavigator(new TimelineQueueNavigator(mediaSession) {
    @Override
    public MediaDescriptionCompat getMediaDescription(Player player, int windowIndex) {
        Bundle extras = new Bundle();
        extras.putInt(MediaMetadataCompat.METADATA_KEY_DURATION, -1);

        return new MediaDescriptionCompat.Builder()
                .setMediaId(trackModel.mediaId)
                .setIconBitmap(trackModel.bitmap)
                .setTitle(trackModel.title)
                .setDescription(trackModel.description)
                .setExtras(extras)
                .build();
    }
});

Upvotes: 3

Related Questions