为什么MediaSession不改变锁屏背景(可能是位图的大小问题:阅读获取更多信息)?(Why M

2019-09-26 06:30发布

我被困在这个问题从去年6天通过媒体会话深入研究之后,我发现这个重要防线

  • 专辑图片为在锁定屏幕上显示。 该图像是用320x320dp的最大尺寸(如果较大的,它的按比例缩小)的位图。

现在,这里是我的媒体会话和通知的代码...

媒体会话

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

    });
}

元数据的方法...

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

现在通知 ....

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

所有的东西都是一样的锁屏等上控制工作正常,但唯一的问题是不会改变设备的锁屏壁纸

我怎样才能做到这一点?OR

如何我可以检查该位图DP尺寸或缩小位图所需的DP大小

随便问问更多信息

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