问题
I am using MediaPlayer
for playing audio in my android application.
There are some troubles with displaying the Now Playing Card in Recommendation row on Leanback Launcher.
I have extended MediaSessionCompat.Callback
in the player.
What am I doing wrong? (code is below). What is supposed to be done for this card to be displayed?
private void createMediaSession() {
if (mSession == null) {
mSession = new MediaSessionCompat(mContext, "MediaSession");
mSession.setCallback(this);
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
mSession.setActive(true);
Intent intent = new Intent(mContext.getApplicationContext(), PlayerActivity.class);
PendingIntent pi = PendingIntent.getActivity(mContext.getApplicationContext(), 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mSession.setSessionActivity(pi);
}
}
private void releaseMediaSession() {
if (mSession != null)
mSession.release();
}
private void updatePlaybackState() {
PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
.setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE);
int state = PlaybackStateCompat.STATE_PLAYING;
if (!isPlaying()) {
state = PlaybackStateCompat.STATE_PAUSED;
}
stateBuilder.setState(state, PlayerInst.getInstance().mMediaPlayer.getCurrentPosition(), 1.0f);
mSession.setPlaybackState(stateBuilder.build());
}
public void updateMetadata() {
final MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mPlaylist.get(playlistIndex).getTitle());
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, mPlaylist.get(playlistIndex).getLink());
if (PlayerInst.getInstance().mMediaPlayer != null)
metadataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, PlayerInst.getInstance().mMediaPlayer.getDuration());
// And at minimum the title and artist for legacy support
metadataBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, mPlaylist.get(playlistIndex).getTitle());
Glide.with(mContext)
.load(Uri.parse(mPlaylist.get(playlistIndex).getLink()))
.asBitmap()
.into(new SimpleTarget<Bitmap>(500, 500) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
metadataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap);
mSession.setMetadata(metadataBuilder.build());
}
});
}
来源:https://stackoverflow.com/questions/34454894/how-to-display-now-playing-card-on-android-tv