我正在尝试将ExoPlayer与MediaSession结合使用,以使用播放控件并在锁屏上显示专辑封面。这一切都在服务中。
控件工作正常,但专辑封面未显示为锁屏背景。它确实显示在通知中。
AudioController跟踪主应用程序UI中正在播放的曲目,与通知/锁屏无关,因此并不重要。
这里的代码只是为了测试一切是否正常,因此专辑封面是一种硬编码资源,等等。
我在这里想念什么?
@Override
public void onCreate() {
super.onCreate();
//Player
TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(trackSelectionFactory));
// Notification manager
playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
context,
PLAYBACK_CHANNEL_ID,
R.string.playback_channel_name,
PLAYBACK_NOTIFICATION_ID,
new MediaDescriptionAdapter() {
@Override
public String getCurrentContentTitle(Player player) {
return AudioController.getInstance().getCurrentTitle();
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
Intent intent = new Intent(context, AudioPlayerService.class);
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@Nullable
@Override
public String getCurrentContentText(Player player) {
return AudioController.getInstance().getCurrentText();
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player, BitmapCallback callback) {
return getBitmap(
context, R.drawable.h1);
}
}
);
playerNotificationManager.setNotificationListener(new NotificationListener() {
@Override
public void onNotificationStarted(int notificationId, Notification notification) {
startForeground(notificationId, notification);
}
@Override
public void onNotificationCancelled(int notificationId) {
stopSelf();
}
});
// omit skip previous and next actions
playerNotificationManager.setUseNavigationActions(true);
// omit fast forward action by setting the increment to zero
playerNotificationManager.setFastForwardIncrementMs(0);
// omit rewind action by setting the increment to zero
playerNotificationManager.setRewindIncrementMs(0);
playerNotificationManager.setPlayer(player);
// Media session
mediaSession = new MediaSessionCompat(context, "TestAudio");
mediaSession.setActive(true);
mediaSessionConnector = new MediaSessionConnector(mediaSession);
mediaSessionConnector.setQueueNavigator(new TimelineQueueNavigator(mediaSession) {
@Override
public MediaDescriptionCompat getMediaDescription(int windowIndex) {
return updateMediaSessionMetaData();
}
});
mediaSessionConnector.setPlayer(player, null);
PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_SEEK_TO |
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_STOP
)
.setState(
PlaybackStateCompat.STATE_PAUSED,
0,
1.0f)
.build();
mediaSession.setPlaybackState(playbackStateCompat);
playerNotificationManager.setMediaSessionToken(mediaSession.getSessionToken());
// Binder
binder = new AudioBinder();
}
private MediaDescriptionCompat updateMediaSessionMetaData() {
MediaMetadataCompat.Builder metaDataBuilder = new MediaMetadataCompat.Builder();
Bitmap albumArtBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.h1);
metaDataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArtBitmap);
metaDataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART, albumArtBitmap);
mediaSession.setMetadata(metaDataBuilder.build());
MediaDescriptionCompat description = metaDataBuilder.build().getDescription();
return description;
}