Why MediaSession not changing the lock screen background (may be Bitmap size problem: read for more info)?

南笙酒味 提交于 2019-12-11 10:38:34

问题


I am stuck in this problem from last 6 days after deep research through media session I found this important line

  • Album artwork for display on the lock screen. The image is a bitmap with a maximum size of 320x320dp (if larger, it's scaled down).

Now, Here is my code of media session and Notification...

MEDIA SESSION

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initMediaSession()  {
    if (mediaSessionManager != null) return;
    mediaSessionManager = (android.media.session.MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
    mediaSession = new MediaSessionCompat(getApplicationContext(), "AudioPlayer");

    transportControls = mediaSession.getController().getTransportControls();

    mediaSession.setActive(true);

    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    updateMetaData();

//=============callBacks of Methods==============================//

    mediaSession.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public void onPlay() {
            play();
            super.onPlay();
        }


        @Override
        public void onSkipToNext() {
            next();
            super.onSkipToNext();
        }

        @Override
        public void onSkipToPrevious() {
            back();     
            super.onSkipToPrevious();
        }

    });
}

method of METADATA...

 public void updateMetaData(){

    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
    builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, Artist);
    builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, Album);
    builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, Title);
   // builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM, bitmap1);
    builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap1);
    mediaSession.setMetadata(builder.build());
}

Now the NOTIFICATION....

public void BuildNotificatio(){

//============Getting Bitmap for setting large icon==========//

    Bitmap bitmap;
    MediaMetadataRetriever   mmr = new MediaMetadataRetriever();
    mmr.setDataSource(PATH.get(position));
    byte[] data = mmr.getEmbeddedPicture();
    if (data != null) {
         bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

    } else {

   bitmap=BitmapFactory.decodeResource(getResources(),
          R.drawable.example_picture);
    }
//==========================================================//
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
        Notification builder = new NotificationCompat.Builder(this, 
notification.CHANNEL_ID_ONE)
                .setShowWhen(false)
                .setStyle(new 
android.support.v4.media.app.NotificationCompat.MediaStyle()
                .setMediaSession(mediaSession.getSessionToken())
                        .setShowActionsInCompactView(0,1,2)
                .setShowCancelButton(true))
                .setSmallIcon(android.R.drawable.stat_sys_headset)
                .setColor(getResources().getColor(R.color.colorAccent))
                .setContentTitle(Title)
                .setContentText(Artist)
                .setLargeIcon(bitmap)

.addAction(android.R.drawable.ic_media_previous,"",backIntent)
                .addAction(index,"",playIntent)
                .addAction(android.R.drawable.ic_media_next,"",nextIntent)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .build();
        startForeground(Notification_ID,builder);
    }else{
        Notification notification = new NotificationCompat.Builder(this)
                .setStyle(new 
android.support.v4.media.app.NotificationCompat.MediaStyle()
                .setMediaSession(mediaSession.getSessionToken()))
                .setShowWhen(false)
                .setSmallIcon(android.R.drawable.stat_sys_headset)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLargeIcon(bitmap)
                .build();
        startForeground(Notification_ID,notification);
    }
}

All things are working fine like controls on lock screen etc, But the only problem is not changing the lock screen wallpaper of device

How can I achieve this... OR

How can I check the bitmap dp size or scaled Down Bitmap to required dp size

Feel Free To Ask For More Info

来源:https://stackoverflow.com/questions/54742921/why-mediasession-not-changing-the-lock-screen-background-may-be-bitmap-size-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!