0

MediaPlayer用于在我的 android 应用程序中播放音频。

在 Leanback 启动器的推荐行中显示“正在玩牌”时存在一些问题。

我已经MediaSessionCompat.Callback在播放器中扩展了。

我究竟做错了什么?(代码如下)。应该怎么做才能显示这张卡?

    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());
                }
            });
}
4

0 回答 0