0

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());
                }
            });
}
Stepan Mazokha
  • 459
  • 1
  • 5
  • 22
  • It looks ok to me, Now Playing Card will appear when you set MediaMetadata to Media session. It is done in `mSession.setMetadata(metadataBuilder.build());`. For reference, you can find more info at [MediaSession & MediaController](http://corochann.com/android-tv-application-hands-on-tutorial-9-195.html). – corochann Dec 28 '15 at 11:32
  • How about changing `MediaSessionCompat` to `MediaSession`? [MediaSession](http://developer.android.com/reference/android/media/session/MediaSession.html) class is introduced in API 21 but Android TV device is introduced from API 21, so it should be ok. – corochann Dec 28 '15 at 11:46
  • Be sure to call `updateMetadata()` – Mario Velasco Dec 16 '16 at 10:38

0 Answers0