Skip to content
Advertisement

Android NowPlaying MediaSession Lock Screen (Samsung)

I’m working on a Audio app based on ExoPlayer, I have implemented the MediaSession controls for a NowPlaying bar in Notification Center and Lock Screen.

The NowPlaying bar in the Notification Center is working on every phone, but on some Samsung phones (Android 11) the NowPlaying bar is not visible on the Lock Screen.

Does any one knows how to fix this? I’m trying to fix this for a week now, and nothing works…

Advertisement

Answer

I saw this problem last week and it was solved with the code below

MediaSessionCompat mediaSession = new MediaSessionCompat(this, MEDIA_SESSION_TAG);
    mediaSession.setActive(true);
    mediaSession.setMetadata(
            new MediaMetadataCompat.Builder()
                    .putString(MediaMetadata.METADATA_KEY_TITLE, title)
                    .putString(MediaMetadata.METADATA_KEY_ARTIST, author)
                    .build()
    );

Add notification code like below or just add .setMediaSession(mediaSession.getSessionToken())) for MediaStyle

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .addAction(iconFavorite, "favorite", pendingFavoriteIntent)
            .addAction(R.drawable.ic_notification_prev, "prev", pendingPrevIntent)
            .addAction(iconPlayPause, titlePlayPause, pendingToggleIntent)
            .addAction(R.drawable.ic_notification_next, "next", pendingNextIntent)
            .addAction(R.drawable.ic_close, "cancel", pendingCancelIntent)
            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(1, 2, 3)
                    .setShowCancelButton(true)
                    .setMediaSession(mediaSession.getSessionToken()))
            .setSmallIcon(R.drawable.ic_notification_play)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setVibrate(new long[]{0L})
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentTitle(title)
            .setContentText(author)
            .setSubText(title)
            .setContentIntent(selectPendingIntent)
            .setLargeIcon(bm)
            .build();
Advertisement