无法将支持 v4 MediaSession 令牌与通知一起使用 MediaStyle.setMediaSession
Can't use Support v4 MediaSession Token with Notification MediaStyle.setMediaSession
我正在编写一些 Android 代码,我不想构建 MediaStyle 通知。我已经在 m mediaplayer 和 mediasession 的大部分中使用了 AppCompat,而我还没有使用的部分我正计划进行切换,这样我就可以保持 4.x 兼容性。
问题?好吧,我正在尝试制作我的 MediaStyle 通知,并给它一个 MediaSession 令牌。我的支持。v4.media.session.MediaSession.Token 似乎与 media.session.MediaSession.Token
不兼容
我已经尝试过铸造,只是保持原始状态。老实说,我很困惑,因为文档说它们是兼容的。
如果您需要其余代码,code can be found 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();
神奇。有一个 Token.getToken();
你需要用到它。
话又说回来,MediaStyle Notifications 只兼容 API 21,祝你好运。
有可能。
检查你的导入,也许你正在导入错误的版本。
- MediaStyle 应该
android.support.v7.app.NotificationCompat.MediaStyle
- NotificationBuilder 应该是
android.support.v7.app.NotificationCompat.Builder
- 通知应该兼容
android.support.v4.app.NotificationCompat
如果您要支持早于 21 的版本,您需要使用兼容 类(所有兼容 类 而不是 "normal")。
这可能对谁有帮助。
首先,您需要导入 v4 MediaSessionCompat 并像这样评论通用 MediaSession:
//import android.media.session.MediaSession;
import android.support.v4.media.session.MediaSessionCompat;
在您的代码中,您需要像这样使用 MediaSessonCompat:
MediaSessionCompat mediaSession = new MediaSessionCompat(getApplicationContext(), "session tag");
MediaSessionCompat.Token token = mediaSession.getSessionToken();
mediaStyle.setMediaSession(token);
我正在编写一些 Android 代码,我不想构建 MediaStyle 通知。我已经在 m mediaplayer 和 mediasession 的大部分中使用了 AppCompat,而我还没有使用的部分我正计划进行切换,这样我就可以保持 4.x 兼容性。
问题?好吧,我正在尝试制作我的 MediaStyle 通知,并给它一个 MediaSession 令牌。我的支持。v4.media.session.MediaSession.Token 似乎与 media.session.MediaSession.Token
不兼容我已经尝试过铸造,只是保持原始状态。老实说,我很困惑,因为文档说它们是兼容的。
如果您需要其余代码,code can be found 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();
神奇。有一个 Token.getToken();
你需要用到它。
话又说回来,MediaStyle Notifications 只兼容 API 21,祝你好运。
有可能。
检查你的导入,也许你正在导入错误的版本。
- MediaStyle 应该
android.support.v7.app.NotificationCompat.MediaStyle
- NotificationBuilder 应该是
android.support.v7.app.NotificationCompat.Builder
- 通知应该兼容
android.support.v4.app.NotificationCompat
如果您要支持早于 21 的版本,您需要使用兼容 类(所有兼容 类 而不是 "normal")。
这可能对谁有帮助。
首先,您需要导入 v4 MediaSessionCompat 并像这样评论通用 MediaSession:
//import android.media.session.MediaSession;
import android.support.v4.media.session.MediaSessionCompat;
在您的代码中,您需要像这样使用 MediaSessonCompat:
MediaSessionCompat mediaSession = new MediaSessionCompat(getApplicationContext(), "session tag");
MediaSessionCompat.Token token = mediaSession.getSessionToken();
mediaStyle.setMediaSession(token);