gpt4 book ai didi

android - 无法将支持 v4 MediaSession token 与 Notification MediaStyle.setMediaSession 一起使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:56 25 4
gpt4 key购买 nike

我正在编写一些 Android 代码,我不想构建 MediaStyle 通知。我已经将 AppCompat 用于 m 媒体播放器和媒体 session 的大部分,而我还没有使用的部分我计划进行切换,以便保持 4.x 兼容性。

问题?好吧,我正在尝试制作我的 MediaStyle 通知,并给它一个 MediaSession token 。我的 support.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,祝你好运。

关于android - 无法将支持 v4 MediaSession token 与 Notification MediaStyle.setMediaSession 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29701637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com