11

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

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

每次歌曲更改时,我首先清除当前MediaMetadataCompat等:PlaybackStateCompatMediaSessionCompat

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我的媒体通知方法对锁定屏幕上显示的专辑封面没有影响。这使它显示在通知本身中,而不是作为锁定屏幕背景。

4

1 回答 1

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

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

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

于 2017-02-20T19:52:01.337 回答