Guest User

Untitled

a guest
Mar 18th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. NotificationCompat.Builder builder = MediaStyleHelper.from(this, mediaSession,NOTIFICATION_DEFAULT_CHANNEL_ID);
  2. if (playbackState == PlaybackStateCompat.STATE_PLAYING){
  3. builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, getString(R.string.pause), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
  4. builder.setSmallIcon(R.drawable.ic_play_mini);}
  5. else{
  6. builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, getString(R.string.play), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
  7. builder.setSmallIcon(R.drawable.ic_stop_mini);}
  8.  
  9.  
  10. builder.setStyle(new MediaStyle()
  11. .setShowActionsInCompactView(0)
  12. .setShowCancelButton(true)
  13. .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_STOP))
  14. .setMediaSession(mediaSession.getSessionToken()));
  15. builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
  16. builder.setShowWhen(false);
  17. builder.setPriority(NotificationCompat.PRIORITY_HIGH);
  18. builder.setOnlyAlertOnce(true);
  19. builder.setChannelId(NOTIFICATION_DEFAULT_CHANNEL_ID);
  20.  
  21.  
  22. return builder.build();
  23.  
  24. class MediaStyleHelper {
  25. static NotificationCompat.Builder from(
  26. Context context, MediaSessionCompat mediaSession, String channelId) {
  27. MediaControllerCompat controller = mediaSession.getController();
  28. MediaMetadataCompat mediaMetadata = controller.getMetadata();
  29. MediaDescriptionCompat description = mediaMetadata.getDescription();
  30.  
  31. NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId);
  32. builder
  33. .setContentTitle(description.getTitle())
  34. .setContentText(description.getSubtitle())
  35. .setSubText(description.getDescription())
  36. .setLargeIcon(description.getIconBitmap())
  37. .setContentIntent(controller.getSessionActivity())
  38. .setDeleteIntent(
  39. MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
  40. .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
  41. return builder;
  42. }}
Add Comment
Please, Sign In to add comment