yyunikov
yyunikov

Reputation: 5897

Android TV: PlaybackControlsRow action long press

I'm trying to implement a fast-forward and rewind actions using PlaybackControlsRow using Leanback library for Android TV, however I can't find any method to detect a long press on these buttons. My current implementation is simple, only does seeking for 10 seconds on one click:

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

In PlaybackActivity:

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

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

Is it possible to implement fast-forward and rewind on long press of actions, like key-up/key-down events on the action buttons?

Upvotes: 2

Views: 1413

Answers (1)

yyunikov
yyunikov

Reputation: 5897

After looking for other solutions, seems that PlaybackControlsRowPresenter is designed in the way that it should have no long presses, but instead increasing/reducing speed by the number of clicks on fast-forward/rewind buttons. It can be clearly seen from internal constructor implementation of PlaybackControlsRowPresenter.FastForwardAction and PlaybackControlsRowPresenter.RewindAction classes:

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

So, as the result, my solution for now is increasing/reducing a speed by each click on the fast-forward/rewind buttons (which is done on UI by default). After that, when I click on play - it just resumes video from the seeked point.

UPDATE (updated parts of code):

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

Upvotes: 1

Related Questions