I have an app that reproduces from the service a webradio (using AACDecoder lib) and show on a simple ongoing notification what is playing while the activity is not resumed. What i want is to add by NotificationCompat.MediaStyle a button to stop (and eventually re-start) the playing from notification, without the need of opening the activity. Despite it is a webradio player and not a player with a reprodution list, i need only a play-pause button. But even after reading various guides (like this) im not enable to correctly understand and implement this function, mainly because of the recent deprecations and newly compatibility classes on this scenario.
This is how my notification appears:
And this is how i want (approximately, just need the pause-play button):
Any help is welcome. Thanks in advance!
What you are looking for is more along the collapsed version of the MediaStyle notification. While I'm not sure how to force it to stay collapsed, this should set you on the right path. I used a bit of the code found in the UniversalMusicPlayer that is part of the sample code provided for Android.
Specifically - You want to check out the MediaNotificationManager and MusicService classes.
Both of which are a bit overly complex for what you are trying to achieve, but could be a good place to refer to. Specifically, the createNotification method (see a Play/Pause version below).
private Notification createNotification() {
if (mMetadata == null || mPlaybackState == null) {
return null;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
int playPauseButtonPosition = 0;
addPlayPauseAction(notificationBuilder);
MediaDescriptionCompat description = mMetadata.getDescription();
String fetchArtUrl = null;
Bitmap art = null;
if (description.getIconUri() != null) {
// This sample assumes the iconUri will be a valid URL formatted String, but
// it can actually be any valid Android Uri formatted String.
// async fetch the album art icon
String artUrl = description.getIconUri().toString();
art = ChannelImageCache.getInstance().getBigImage(artUrl);
if (art == null) {
fetchArtUrl = artUrl;
// use a placeholder art while the remote art is being downloaded
art = BitmapFactory.decodeResource(mService.getResources(),
R.drawable.ic_default_art);
}
}
notificationBuilder
.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(
new int[]{playPauseButtonPosition}) // show only play/pause in compact view
.setMediaSession(mSession.getSessionToken()))
.setColor(mNotificationColor)
.setSmallIcon(R.drawable.ic_notification)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setUsesChronometer(true)
.setContentIntent(createContentIntent(description))
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setLargeIcon(art);
setNotificationPlaybackState(notificationBuilder);
if (fetchArtUrl != null) {
fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
}
return notificationBuilder.build();
}
private void addPlayPauseAction(NotificationCompat.Builder builder) {
String label;
int icon;
PendingIntent intent;
if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
label = mService.getString(R.string.mr_media_route_controller_pause);
icon = R.drawable.uamp_ic_pause_white_24dp;
intent = mPauseIntent;
} else {
label = mService.getString(R.string.mr_media_route_controller_play);
icon = R.drawable.uamp_ic_play_arrow_white_24dp;
intent = mPlayIntent;
}
builder.addAction(new NotificationCompat.Action(icon, label, intent));
}
This Notification will use a MediaSession in your service (where I am assuming you AACDecoder/AACPlayer is living). When you get your ID3 Metadata from the player, you'll want to update the Metadata and set it to the mediaSession.
private void updateMetadata() {
final MediaMetadataCompat track = generateMediaMetadataCompat(currentMetaData);
mediaSession.setMetadata(track);
}
private MediaMetadataCompat generateMediaMetadataCompat(SomeMetadataModelObject streamMetadata)
{
currentMediaMetadataCompat = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, streamMetadata.getMediaId())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, streamMetadata.getAlbumName())
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, streamMetadata.getArtistName())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, "http://somealbumart.png" )
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, streamMetadata.getSongTitle())
.build();
return currentMediaMetadataCompat;
}