我无法在锁屏上获取Albumart,我尝试同时使用METADATA_KEY_ALBUM_ART和METADATA_KEY_ART,但没有任何效果。这是我的代码
byte[] data = null;
try {
mmr.setDataSource(music.getNowPlayingList().get(currentIndex).getPath());
data = mmr.getEmbeddedPicture();
} catch (Exception e) {
e.printStackTrace();
}
if (music.getNowPlayingList()!=null && music.getNowPlayingList().size()>music.getIndexOfCurrentSong()) {
GildeUtils.getBitmapFromGlide(this,data , new GlideInterface() {
@Override
public void getBitmap(Bitmap bitmap) {
//Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.nowplaying);
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,bitmap)
.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON,bitmap)
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, music.getNowPlayingList().get(currentIndex).getArtist())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, music.getNowPlayingList().get(currentIndex).getAlbum())
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, music.getNowPlayingList().get(currentIndex).getTitle())
.build());
}
});
}
我还发出了mediastyle通知,还像其他问题中提到的那样,将Albumart设置为大图标,还添加了mediasession.setActive(true)和音频焦点,似乎没有任何作用。
最佳答案
所以最后它对我有用:)添加了Playbackstate代码,现在一切似乎都可以正常工作。
GildeUtils.getBitmapFromGlide(this,data , new GlideInterface() {
@Override
public void getBitmap(Bitmap bitmap) {
if(bitmap==null) {
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nowplaying);
}
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,bitmap)
.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON,bitmap)
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, music.getNowPlayingList().get(currentIndex).getArtist())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, music.getNowPlayingList().get(currentIndex).getAlbum())
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, music.getNowPlayingList().get(currentIndex).getTitle())
.build());
PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder();
isLockscreenUpdated = true;
stateBuilder.setActiveQueueItemId(music.getNowPlayingList().get(music.getIndexOfCurrentSong()).getId());
long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
stateBuilder.setActions(actions);
stateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f);
mediaSession.setPlaybackState(stateBuilder.build());
}
});