1

在通过媒体会议进行深入研究后,我从过去 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);
    }
}

一切正常,如锁定屏幕上的控件等,但唯一的问题是不更改设备的锁定屏幕壁纸

我怎样才能做到这一点......或者

如何检查位图 dp 大小或将位图缩小到所需的 dp 大小

随时询问更多信息

4

0 回答 0