草庐IT

android - MediaMetadataCompat METADATA_KEY_ART 仅在第一次设置图像

coder 2023-11-21 原文

在我的应用程序中,我使用 MediaSessionCompat 来处理从我的媒体播放器服务播放的音频。特别是,我想将当前歌曲的元数据广播到蓝牙设备(有效),并将锁屏图像设置为当前歌曲的专辑封面。

类似于这个问题:Set lock screen background in Android (like Spotify do)

每次歌曲改变时,我首先从 MediaSessionCompat 中清除当前的 MediaMetadataCompatPlaybackStateCompat,如下所示:

mSession.setActive(false);
mSession.setMetadata(null);
mSession.setPlaybackState(null);

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

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();

然后我在 MediaSessionCompat

上设置新的元数据
mSession.setActive(true);
mSession.setMetadata(metadata);
mSession.setPlaybackState(state);

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

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

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());

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

最佳答案

你需要的是一个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);

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

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

关于android - MediaMetadataCompat METADATA_KEY_ART 仅在第一次设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333548/

有关android - MediaMetadataCompat METADATA_KEY_ART 仅在第一次设置图像的更多相关文章

随机推荐