显示总时间android leanback

时间:2017-09-27 09:18:34

标签: android android-video-player leanback

我有一个简单的Android TV leanback应用程序,可以显示视频。视频播放器工作正常并显示必要的控件。但是,我想在PlaybackControlsRow的右侧添加视频的总持续时间,但不知道如何。我正在使用此代码来设置PlaybackControlRow

private void addPlaybackControlsRow() {
        /* movieItem data must have newly allocated area, do not refer existing movie (ex. mItems.get(0))
           since this item will be overwritten later */
        Movie movieItem = new Movie();
        mPlaybackControlsRow = new PlaybackControlsRow(movieItem);
        mRowsAdapter.add(mPlaybackControlsRow);

        ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector();
        mPrimaryActionsAdapter = new ArrayObjectAdapter(presenterSelector);
        mSecondaryActionsAdapter = new ArrayObjectAdapter(presenterSelector);
        mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter);
        mPlaybackControlsRow.setSecondaryActionsAdapter(mSecondaryActionsAdapter);

        Activity activity = getActivity();
        mPlayPauseAction = new PlaybackControlsRow.PlayPauseAction(activity);
        mRepeatAction = new PlaybackControlsRow.RepeatAction(activity);
        mThumbsUpAction = new PlaybackControlsRow.ThumbsUpAction(activity);
        mThumbsDownAction = new PlaybackControlsRow.ThumbsDownAction(activity);
        mShuffleAction = new PlaybackControlsRow.ShuffleAction(activity);
        mSkipNextAction = new PlaybackControlsRow.SkipNextAction(activity);
        mSkipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(activity);
        mFastForwardAction = new PlaybackControlsRow.FastForwardAction(activity);
        mRewindAction = new PlaybackControlsRow.RewindAction(activity);


        /* PrimaryAction setting */
        mPrimaryActionsAdapter.add(mSkipPreviousAction);
        mPrimaryActionsAdapter.add(mRewindAction);
        mPrimaryActionsAdapter.add(mPlayPauseAction);
        mPrimaryActionsAdapter.add(mFastForwardAction);
        mPrimaryActionsAdapter.add(mSkipNextAction);
    }

我设置了当前时间,并尝试设置总时间,如下所示:

int currentTime = mPlaybackController.getCurrentPosition();
int totalTime = mPlaybackControlsRow.getTotalTime();
mPlaybackControlsRow.setCurrentTime(currentTime);
mPlaybackControlsRow.setTotalTime(totalTime);

视频播放器看起来像这样: Current video player

如您所见,我无法获得 PlaybackControlsRow 右侧的总时间。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

尝试getDuration()setDuration(long),因为{C}级别已弃用getTotalTime()setTotalTime()

参考:PlaybackControlsRow

答案 1 :(得分:0)

正如用户@Ewald所评论的那样,我忘了通知mRowsAdapter对象。如果您使用的是25级或更低级别的API,则在setTotalTime(totalTime)对象上调用PlaybackControlsRow时,必须通知适配器以查看更改。以下是setTotalTime方法

的文档
  

设置播放控件行的总时间(以毫秒为单位)。

     

如果在将行绑定到视图后设置,则必须是适配器   通知此行已更改。

因此,我像这样通知了适配器

mRowsAdapter.notifyArrayItemRangeChanged(0, mRowsAdapter.size());

现在我得到了总时间。希望这有助于某人。