Skip to content

Instantly share code, notes, and snippets.

@ianhanniballake
Last active April 7, 2021 03:29
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ianhanniballake/47617ec3488e0257325c to your computer and use it in GitHub Desktop.
Save ianhanniballake/47617ec3488e0257325c to your computer and use it in GitHub Desktop.
package com.example.mediasessioncompat;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.media.MediaDescriptionCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaButtonReceiver;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v7.app.NotificationCompat;
/**
* Helper APIs for constructing MediaStyle notifications
*/
public class MediaStyleHelper {
/**
* Build a notification using the information from the given media session. Makes heavy use
* of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information.
* @param context Context used to construct the notification.
* @param mediaSession Media session to get information.
* @return A pre-built notification with information from the given media session.
*/
public static NotificationCompat.Builder from(
Context context, MediaSessionCompat mediaSession) {
MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
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;
}
}
@Vipul-Chauhan
Copy link

Some of these above methods are depreciated now so anyone can please provide new and complete tutorial for this like
How to set buttons? How to add actions on button click ? How to add buttons on lock screen please check the link https://stackoverflow.com/questions/54633202/how-to-set-mediaplayer-notification-with-mediasession-and-notificationcompat-med?noredirect=1#comment96070177_54633202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment