Set lock screen background in Android (like Spotify do)

前端 未结 6 779
旧巷少年郎
旧巷少年郎 2020-12-02 12:31

I know that this topic has been already discussed here, here and here, and the answer seems to be that it is not possible.

But I recently installed Spotify in my Ne

6条回答
  •  被撕碎了的回忆
    2020-12-02 13:23

    Well, after trying some ways, I have a simple code here; Try using this method;

    private void updateMetaData() {
        mediaSession =new MediaSessionCompat(context,"BXPlayer");
    
        Bitmap cover = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.cover2); 
    
       mediaSession.setMetadata(new MediaMetadataCompat.Builder()
                .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, cover)
                .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, mSelectedSong.getArtist())
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, mSelectedSong.getAlbum())
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, mSelectedSong.getTitle())
                .build());
    }
    

    then in your notification you need to set style to android.support.v4.media.app.NotificationCompat.MediaStyle() and set the media session token to use the current metadata. Check this snippet below;

    builder.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
                .setShowActionsInCompactView(0, 1, 2)
        .setMediaSession(mediaSession.getSessionToken()));
        return builder.build();
    

    To work, you must include implementation "com.android.support:support-v4:$latest_version" in your app build.gradle And boom! you are good to go.

提交回复
热议问题