不能将Support v4 MediaSession令牌与Notification MediaStyle.setMediaSession一起使用

Pix*_*ect 5 android incompatibletypeerror android-appcompat android-notifications android-mediaplayer

我正在处理一些Android代码,并且无法构建MediaStyle通知。我已经在大部分媒体播放器和媒体会话中使用了AppCompat,而我还没有使用的内容我打算进行切换,以便保持4.x兼容性。

问题?好吧,我正在尝试发出MediaStyle通知,并为其提供MediaSession令牌。我的support.v4.media.session.MediaSession.Token似乎与media.session.MediaSession.Token不兼容

我已经尝试过投放,而只保留原始状态。老实说,我很困惑,因为文档说它们是兼容的。

如果您需要其余的代码,可以在这里找到代码

或者,您可以在此处查看相关代码。

    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();
Run Code Online (Sandbox Code Playgroud)

Pix*_*ect 4

神奇。有一个Token.getToken();你需要使用它。

话又说回来,MediaStyle 通知仅兼容 API 21,所以祝你好运。