Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NotificationCompat.Builder builder = MediaStyleHelper.from(this, mediaSession,NOTIFICATION_DEFAULT_CHANNEL_ID);
- if (playbackState == PlaybackStateCompat.STATE_PLAYING){
- builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, getString(R.string.pause), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
- builder.setSmallIcon(R.drawable.ic_play_mini);}
- else{
- builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, getString(R.string.play), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
- builder.setSmallIcon(R.drawable.ic_stop_mini);}
- builder.setStyle(new MediaStyle()
- .setShowActionsInCompactView(0)
- .setShowCancelButton(true)
- .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_STOP))
- .setMediaSession(mediaSession.getSessionToken()));
- builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
- builder.setShowWhen(false);
- builder.setPriority(NotificationCompat.PRIORITY_HIGH);
- builder.setOnlyAlertOnce(true);
- builder.setChannelId(NOTIFICATION_DEFAULT_CHANNEL_ID);
- return builder.build();
- class MediaStyleHelper {
- static NotificationCompat.Builder from(
- Context context, MediaSessionCompat mediaSession, String channelId) {
- MediaControllerCompat controller = mediaSession.getController();
- MediaMetadataCompat mediaMetadata = controller.getMetadata();
- MediaDescriptionCompat description = mediaMetadata.getDescription();
- NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId);
- builder
- .setContentTitle(description.getTitle())
- .setContentText(description.getSubtitle())
- .setSubText(description.getDescription())
- .setLargeIcon(description.getIconBitmap())
- .setContentIntent(controller.getSessionActivity())
- .setDeleteIntent(
- MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
- .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
- return builder;
- }}
Add Comment
Please, Sign In to add comment