2017-02-19 279 views
9

在我的應用程序中,我使用MediaSessionCompat來處理播放來自我的媒體播放器服務的音頻。特別是,我想將當前歌曲的元數據廣播到藍牙設備(可以工作),並將鎖定屏幕圖像設置爲當前歌曲的專輯封面。MediaMetadataCompat METADATA_KEY_ART第一次設置圖像

這個問題類似:Set lock screen background in Android (like Spotify do)

每次換歌,我第一次明確註銷當前MediaMetadataCompatPlaybackStateCompatMediaSessionCompat像這樣:

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方法。這使得它顯示在通知本身中,而不是鎖定屏幕背景。

回答

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

+0

看來,'setVisibility'方法只與安全鎖屏上顯示的信息有關。將此方法添加到我的媒體通知中並沒有改變行爲 –

+0

@AndrewBrooke當然,您應該創建一個**新**通知並在每次信息更改時發佈。 – pantos27

+0

這就是我目前正在做的。通知被更新,但鎖定屏幕上的圖像不是第一次。 –