Reputation: 1336
I'm working on some Android code, and I wan't to build a MediaStyle Notification. I'm already using AppCompat for most of m mediaplayer and mediasession, and what I don't already use I'm planning on switching over just so I can keep 4.x compatibility.
Issue? Well, I'm trying to make my MediaStyle notification, and give it a MediaSession Token. My support.v4.media.session.MediaSession.Token doesn't seem to be compatable with media.session.MediaSession.Token
I've tried casting, and just leaving it raw. I'm honestly confused because the docs say they're compatible.
If you want the rest of the code, the code can be found here
Or you can look at the relevant code here.
Intent nIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent);
notificationManager.notify(notifId, n);
ComponentName c = new ComponentName("com.thefan.android", "BackgroundService");
ms = new MediaSessionCompat(this, "TheFan", c, pIntent);
ms.setMetadata(new MediaMetadataCompat.Builder()
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, artwork)
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Pink Floyd")
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Dark Side of the Moon")
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, "The Great Gig in the Sky")
.build());
// Indicate you're ready to receive media commands
ms.setActive(true);
// Attach a new Callback to receive MediaSession updates
ms.setCallback(new MediaSessionCompat.Callback() {
// Implement your callbacks
});
// Indicate you want to receive transport controls via your Callback
ms.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
// Create a new Notification
final Notification noti = new Notification.Builder(this)
// Hide the timestamp
.setShowWhen(false)
// Set the Notification style
.setStyle(new Notification.MediaStyle()
// Attach our MediaSession token
.setMediaSession(ms.getSessionToken())
// Show our playback controls in the compat view
.setShowActionsInCompactView(0, 1, 2))
// Set the Notification color
.setColor(0xFFDB4437)
// Set the large and small icons
.setLargeIcon(artwork)
.setSmallIcon(R.drawable.your_small_icon)
// Set Notification content information
.setContentText("Pink Floyd")
.setContentInfo("Dark Side of the Moon")
.setContentTitle("The Great Gig in the Sky")
// Add some playback controls
.addAction(R.drawable.your_prev_icon, "prev", retreivePlaybackAction(3))
.addAction(R.drawable.your_pause_icon, "pause", retreivePlaybackAction(1))
.addAction(R.drawable.your_next_icon, "next", retreivePlaybackAction(2))
.build();
Upvotes: 8
Views: 7414
Reputation: 38252
As others have pointed out, the underlying MediaSession.Token can be obtained through MediaSessionCompat.getToken()
, and its type can safely be cast. In Kotlin:
val mediaStyle = Notification.MediaStyle()
.setMediaSession(mediaSessionCompat.sessionToken.token as MediaSession.Token?)
Upvotes: 1
Reputation: 1520
For whom this may be helpful.
Firstly you need import v4 MediaSessionCompat and comment the general MediaSession like this:
//import android.media.session.MediaSession;
import android.support.v4.media.session.MediaSessionCompat;
In your code, you need use MediaSessonCompat like this:
MediaSessionCompat mediaSession = new MediaSessionCompat(getApplicationContext(), "session tag");
MediaSessionCompat.Token token = mediaSession.getSessionToken();
mediaStyle.setMediaSession(token);
Upvotes: 3
Reputation: 15936
It is posible.
Check your imports, maybe you are importing bad versions.
android.support.v7.app.NotificationCompat.MediaStyle
android.support.v7.app.NotificationCompat.Builder
android.support.v4.app.NotificationCompat
If you are going to support older version than 21 you need to use Compat classes (ALL compat classes instead of "normal" ones).
Upvotes: -1
Reputation: 1336
Magical. There's a Token.getToken();
You need to use that.
Then again, MediaStyle Notifications are only API 21 compatible, so good luck.
Upvotes: 4