2

我正在尝试PlaybackControlsRow使用Leanback libraryfor实现快进和快退操作Android 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));
}

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

4

1 回答 1

1

在寻找其他解决方案之后,似乎它PlaybackControlsRowPresenter的设计方式不应长按,而是通过点击快进/快退按钮的次数来增加/减少速度。PlaybackControlsRowPresenter.FastForwardAction从和PlaybackControlsRowPresenter.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);
    }
于 2017-03-19T20:25:29.000 回答