-
-
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; | |
} | |
} |
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!
@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
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.
@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.
You can omit these two unused imports.
import android.app.PendingIntent;
import android.content.Intent;
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"
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
Note: the
getActionIntent
method was deprecated with the release ofMediaButtonReceiver.buildMediaButtonPendingIntent()
, which has the additional benefit of taking aPlaybackStateCompat.ACTION_
constant, avoiding the need to determine the rightKeyEvent
for each action.