Android TV:PlaybackControlsRow动作长按

时间:2017-03-11 18:39:18

标签: android android-tv leanback mediaplayback

我正在尝试使用PlaybackControlsRow使用Leanback libraryAndroid TV实施快进和快退操作,但我找不到任何方法来检测长按这些纽扣。我目前的实现很简单,只需一次点击即可寻找10秒:

private void setupRows() {
    final ClassPresenterSelector ps = new ClassPresenterSelector();
    final PlaybackControlsRowPresenter playbackControlsRowPresenter =
            new PlaybackControlsRowPresenter(new DescriptionPresenter());

    playbackControlsRowPresenter.setOnActionClickedListener(action -> {
        if (action.getId() == playPauseAction.getId()) {
            togglePlayback(playPauseAction.getIndex() == PlayPauseAction.PLAY);
        } else if (action.getId() == fastForwardAction.getId()) {
            fastForward();
            return;
        } else if (action.getId() == rewindAction.getId()) {
            rewind();
            return;
        }
        if (action instanceof PlaybackControlsRow.MultiAction) {
            ((PlaybackControlsRow.MultiAction) action).nextIndex();
            notifyChanged(action);
        }
    });

    ps.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter);
    ps.addClassPresenter(ListRow.class, new ListRowPresenter());
    rowsAdapter = new ArrayObjectAdapter(ps);

    updatePlaybackControlsRow();

    setAdapter(rowsAdapter);
}

private void fastForward() {
    ((PlaybackActivity) getActivity()).onFragmentFastForward();

    final int currentTime = ((PlaybackActivity) getActivity()).getPosition();
    playbackControlsRow.setCurrentTime(currentTime);
}

private void rewind() {
    ((PlaybackActivity) getActivity()).onFragmentRewind();

    final int currentTime = ((PlaybackActivity) getActivity()).getPosition();
    playbackControlsRow.setCurrentTime(currentTime);
}

PlaybackActivity

public void onFragmentFastForward() {
    // Fast forward 10 seconds.
    videoView.seekTo(videoView.getCurrentPosition() + (10 * 1000));
}

public void onFragmentRewind() {
    videoView.seekTo(videoView.getCurrentPosition() - (10 * 1000));
}

是否可以在长按动作时实现快进和快退,例如动作按钮上的按键/按键事件?

1 个答案:

答案 0 :(得分:1)

在寻找其他解决方案之后,似乎PlaybackControlsRowPresenter的设计应该没有长按,而是通过快进/快退按钮的点击次数来增加/减少速度。从PlaybackControlsRowPresenter.FastForwardActionPlaybackControlsRowPresenter.RewindAction类的内部构造函数实现可以清楚地看到它:

/**
 * Constructor
 * @param context Context used for loading resources.
 * @param numSpeeds Number of supported fast forward speeds.
 */
public FastForwardAction(Context context, int numSpeeds) {

因此,结果,我现在的解决方案是通过每次点击快进/快退按钮来增加/降低速度(默认情况下在UI上完成)。之后,当我点击播放时 - 它只是从搜索到的点恢复视频。

更新(代码的更新部分):

    private void setupRows() {
        final ClassPresenterSelector ps = new ClassPresenterSelector();
        final PlaybackControlsRowPresenter playbackControlsRowPresenter =
                new PlaybackControlsRowPresenter(new DescriptionPresenter());

        playbackControlsRowPresenter.setOnActionClickedListener(action -> {
            if (action.getId() == playPauseAction.getId()) {
                togglePlayback(playPauseAction.getIndex() == PlayPauseAction.PLAY);
                ((PlaybackControlsRow.MultiAction) action).nextIndex();
                notifyChanged(action);
            } else if (action.getId() == fastForwardAction.getId()) {
                if (currentSpeed <= MAX_SPEED) {
                    currentSpeed++;

                    showTogglePlayback(false, true);

                    if (currentSpeed < 0) {
                        prevRewind();
                    } else if (currentSpeed > 0) {
                        nextFastForward();
                    } else {
                        currentSpeed++;
                        prevRewind();
                        nextFastForward();
                    }

                    ((PlaybackActivity) getActivity()).seek(currentSpeed);
                }
            } else if (action.getId() == rewindAction.getId()) {
                if (currentSpeed >= MIN_SPEED) {
                    currentSpeed--;

                    showTogglePlayback(false, true);

                    if (currentSpeed > 0) {
                        prevFastForward();
                    } else if (currentSpeed < 0) {
                        nextRewind();
                    } else {
                        currentSpeed--;
                        prevFastForward();
                        nextRewind();
                    }

                    ((PlaybackActivity) getActivity()).seek(currentSpeed);
                }
            } else if (action.getId() == R.id.lb_control_picture_in_picture &&
                       PlaybackActivity.supportsPictureInPicture(getActivity())) {
                getActivity().enterPictureInPictureMode();
            }
        });

        ps.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter);
        ps.addClassPresenter(ListRow.class, new ListRowPresenter());
        rowsAdapter = new ArrayObjectAdapter(ps);

        updatePlaybackControlsRow();

        setAdapter(rowsAdapter);
    }

    private void prevFastForward() {
        fastForwardAction.setIndex(fastForwardAction.getIndex() - 1);
        notifyChanged(fastForwardAction);
    }

    private void nextFastForward() {
        fastForwardAction.nextIndex();
        notifyChanged(fastForwardAction);
    }

    private void prevRewind() {
        rewindAction.setIndex(rewindAction.getIndex() - 1);
        notifyChanged(rewindAction);
    }

    private void nextRewind() {
        rewindAction.nextIndex();
        notifyChanged(rewindAction);
    }