MediaMetadataCompat METADATA_KEY_ART仅在第一次设置图像

And*_*oke 11 android android-mediasession

在我的应用程序中,我正在利用MediaSessionCompat媒体播放器服务播放音频.特别是,我想将当前歌曲的元数据广播到蓝牙设备(可以工作),并将锁定屏幕图像设置为当前歌曲的专辑封面.

与此问题类似:在Android中设置锁定屏幕背景(如Spotify do)

每次换歌,我先理清当前MediaMetadataCompatPlaybackStateCompatMediaSessionCompat像这样:

mSession.setActive(false);
mSession.setMetadata(null);
mSession.setPlaybackState(null);
Run Code Online (Sandbox Code Playgroud)

然后,我使用各自的构建器创建这些类的新实例

MediaMetadataCompat metadata = new MediaMetadataCompat.Builder()
        .putString(MediaMetadataCompat.METADATA_KEY_TITLE,
                                songName)
        .putString(MediaMetadataCompat.METADATA_KEY_ARTIST,
                                artistName)
        .putString(MediaMetadataCompat.METADATA_KEY_ALBUM,
                                albumName)
        .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, durationMs)
        .putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap)
        .build();

PlaybackStateCompat state = new PlaybackStateCompat.Builder()
        .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE |
                                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
        .setState(PlaybackStateCompat.STATE_PLAYING, positionMs, 1.0f, SystemClock.elapsedRealtime())
        .build();
Run Code Online (Sandbox Code Playgroud)

然后我在上面设置新的元数据 MediaSessionCompat

mSession.setActive(true);
mSession.setMetadata(metadata);
mSession.setPlaybackState(state);
Run Code Online (Sandbox Code Playgroud)

在我的蓝牙设备上,元数据工作正常,并且每次歌曲更改时都会更改.然而,在我的手机上,锁屏专辑封面仅在第一次更新.我已经确认我设置的位图是新的,但图像不会改变.

我还在服务中创建媒体样式通知,以允许用户通过持久通知和锁定屏幕控制音乐.

NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();
style.setShowActionsInCompactView(0, 1, 2, 3, 4);

Intent intent = new Intent(this, DestinationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentIntent(pendingIntent)
            .setStyle(style)
            .setContentTitle(songName)
            .setContentText(artistName)
            .setLargeIcon(bitmap);

// Code to set notification actions 

startForeground(NOTIFICATION_ID_PLAYER_CONTROLS, builder.build());
Run Code Online (Sandbox Code Playgroud)

但是,setLargeIcon我的媒体通知方法对锁定屏幕上显示的专辑封面没有影响.这使它显示在通知本身,但不是锁屏背景.

pan*_*s27 1

您需要的是MediaStyle通知

MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setContentTitle(description.getTitle())
   .setContentText(description.getSubtitle())
   .setSubText(description.getDescription())
   .setLargeIcon(description.getIconBitmap())
   .setContentIntent(controller.getSessionActivity())
   .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,PlaybackStateCompat.ACTION_STOP))
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
Run Code Online (Sandbox Code Playgroud)

VISIBILITY_PUBLIC值将使您在此处设置的信息在锁定屏幕上可见

有关更多信息,请参阅 @ianhanniballake 的要点 https://gist.github.com/ianhanniballake/47617ec3488e0257325c