2017-09-04 107 views
1

将所有当前歌曲信息加载到会话元数据后,专辑封面将拒绝加载锁定屏幕。我知道位图是有效的,因为我为通知艺术使用了相同的资源。我也尝试过使用静态资源,但没有运气。任何帮助将非常感激。Android MediaSession不会将专辑封面显示为锁屏背景

 String more; 
    if (shuffled) { 
     more = Integer.toString(shufflePosition+1) + "/" + Integer.toString(playlist.size()); 
    } 
    else{ 
     more = Integer.toString(position+1) + "/" + Integer.toString(playlist.size()); 
    } 

    int notificationAction = android.R.drawable.ic_media_pause;//needs to be initialized 

    //Build a new notification according to the current state of the MediaPlayer 
    if (mp.isPlaying()) { 
     notificationAction = android.R.drawable.ic_media_pause; 
    } else{ 
     notificationAction = android.R.drawable.ic_media_play; 
    } 

    Bitmap bitmap = null; 
    try { 
     bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),np.getArt()); 
    } catch (IOException e) { 
     bitmap = BitmapFactory.decodeResource(getResources(), 
       R.mipmap.empty_track); 
     e.printStackTrace(); 
    } 

    mSession.setMetadata(new MediaMetadataCompat.Builder() 
      .putBitmap(MediaMetadata.METADATA_KEY_ART, bitmap) 
      .putString(MediaMetadata.METADATA_KEY_ARTIST, np.getArtist()) 
      .putString(MediaMetadata.METADATA_KEY_ALBUM, np.getAlbum()) 
      .putString(MediaMetadata.METADATA_KEY_TITLE, np.getTrack()) 
      .build()); 

    Notification notification; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     notification = new android.support.v7.app.NotificationCompat.Builder(this) 
       .setVisibility(Notification.VISIBILITY_PUBLIC) 
       .setSmallIcon(R.mipmap.empty_artist) 
       .setShowWhen(false) 
       .addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_previous, "Previous", pendingPrev).build()) 
       .addAction(new NotificationCompat.Action.Builder(notificationAction, "Pause", pendingPlay).build()) 
       .addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_next, "Next", pendingNext).build()) 
       .setColor(getResources().getColor(R.color.colorPrimary)) 
       .setContentTitle(np.getTrack()) 
       .setContentText(np.getArtist()) 
       .setSubText(more) 
       .setPriority(Notification.PRIORITY_MAX) 
       .setLargeIcon(bitmap) 
       .setContentIntent(launchActivity) 
       .setStyle(new android.support.v7.app.NotificationCompat.MediaStyle() 
         .setMediaSession(mSession.getSessionToken()).setShowActionsInCompactView(0,1,2)) 
       .build(); 
    } else { 
     notification = new android.support.v7.app.NotificationCompat.Builder(this) 
       .setVisibility(Notification.VISIBILITY_PUBLIC) 
       .setSmallIcon(R.mipmap.empty_artist) 
       .setShowWhen(false) 
       .addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_previous, "Previous", pendingPrev).build()) 
       .addAction(new NotificationCompat.Action.Builder(notificationAction, "Pause", pendingPlay).build()) 
       .addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_media_next, "Next", pendingNext).build()) 
       .setColor(getResources().getColor(R.color.colorPrimary)) 
       .setContentTitle(np.getTrack()) 
       .setContentText(np.getArtist()) 
       .setSubText(more) 
       .setPriority(Notification.PRIORITY_MAX) 
       .setLargeIcon(bitmap) 
       .setContentIntent(launchActivity) 
       .setStyle(new android.support.v7.app.NotificationCompat.MediaStyle() 
         .setMediaSession(mSession.getSessionToken()) 
         .setShowActionsInCompactView(0,1,2)) 
       .build(); 
    } 
    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(001, notification); 
    startForeground(001, notification); 
+0

你没有设置'PlaybackState'以及元数据集在MediaMetadataCompat背景位图? –

回答