Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@ianhanniballake

This comment has been minimized.

Copy link
Owner Author

ianhanniballake commented Sep 5, 2016

Note: the getActionIntent method was deprecated with the release of MediaButtonReceiver.buildMediaButtonPendingIntent(), which has the additional benefit of taking a PlaybackStateCompat.ACTION_ constant, avoiding the need to determine the right KeyEvent for each action.

@Bradleycorn

This comment has been minimized.

Copy link

Bradleycorn commented Oct 10, 2016

Thanks for this great piece of boilerplate. I'm adding some actions to the notification via some statements like:

builder.addAction(R.drawable.ic_pause_24dp, context.getString(R.string.label_pause), MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_SKIP_TO_NEXT));

This works great, most of the time, and pressing these buttons results in the appropriate MediaSessionCompat.Callback to be called. But, when create an action and use PlaybackStateCompat.ACTION_PAUSE, and then click on the cooresponding action button in my mediastyle notification (while my mediasession is active and music is playing), the onPause() of my MediaSessionCompat.Callback doesn't get called. Has anyone else seen this?

SOLVED! I forgot to include ACTION_PAUSE as a valid action (via PlaybackStateCompat.setActions()) when setting the playback state on my media session. Whoops! Works great now!

@vyshnavkr

This comment has been minimized.

Copy link

vyshnavkr commented Jan 27, 2017

@ianhanniballake
I am unable to call getDuration from mediaBrowserService. Eariler I used boundService with which I could access the public methods easily
Can you help with this:
http://stackoverflow.com/q/41888988/6737655

@cbaggers

This comment has been minimized.

Copy link

cbaggers commented May 29, 2017

To those who came here looking for something that would work on android versions older than 24.2.0, check the revisions for this gist.

@daveabes

This comment has been minimized.

Copy link

daveabes commented Jun 26, 2017

@ianhanniballake
Is it possible at all to use RemoteViews for the small/large style notification layout and still keep the lock screen meta from dissapearing from the lock screen? When I apply a customBigNotification style my lock screen meta data is lost for some reason.

@valdezalbertm

This comment has been minimized.

Copy link

valdezalbertm commented Mar 26, 2018

You can omit these two unused imports.
import android.app.PendingIntent;
import android.content.Intent;

@i-me-mine

This comment has been minimized.

Copy link

i-me-mine commented Sep 2, 2018

this line causes my mediaplayer to throw error (100,0) and an ANR popup after a few seconds...

.addAction(R.android.drawable.ic_media_pause, "Pause", MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PAUSE));

I skirted the problem by building my own pending intents and handling them with with the transport controls in my connected fragment...

but i can't find anything that would explain why...

anybody have an idea?

noting "Note: the getActionIntent method was deprecated with the release of MediaButtonReceiver.buildMediaButtonPendingIntent(), which has the additional benefit of taking a PlaybackStateCompat.ACTION_ constant, avoiding the need to determine the right KeyEvent for each action."

it might be a case of "that's not a bug, that's a feature"

@Vipul-Chauhan

This comment has been minimized.

Copy link

Vipul-Chauhan commented Feb 12, 2019

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
You can’t perform that action at this time.