Android MediaSession未将专辑封面显示为锁屏背景

时间:2017-09-04 00:57:10

标签: java android

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

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

2 个答案:

答案 0 :(得分:2)

根据Working with a Media Session documentation page

  

在Android 4.0(API级别14)及更高版本中,锁定屏幕的背景显示您的专辑图片 - 但前提是媒体会话元数据包含背景位图。

您必须使用putBitmapMETADATA_KEY_ALBUM_ART

使用METADATA_KEY_ARTMediaMetadataCompat中设置背景位图

答案 1 :(得分:0)

我发现这3天后必须使用播放状态

    public void pausePlayer() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            startForeground(false);
            ((MainActivity) context).notificationUpdated(false);
            isPlay = false;
            playbackStateCompat.setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_STOP);
            playbackStateCompat.setState(PlaybackStateCompat.STATE_PAUSED, mediaPlayer.getCurrentPosition(), 1.0f, SystemClock.elapsedRealtime());
            mediaSession.setPlaybackState(playbackStateCompat.build());
        }
    }
  mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(currentSong.getUrl()));
        mediaSession.setMetadata(getMetadata());
        playbackStateCompat.setActions(PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_STOP);
        playbackStateCompat.setState(PlaybackStateCompat.STATE_PLAYING, mediaPlayer.getCurrentPosition(), 1.0f, SystemClock.elapsedRealtime());
        mediaSession.setPlaybackState(playbackStateCompat.build());
        mediaPlayer.start();