控制MediaController进度滑块

coc*_*oco 2 android video-streaming media-player mediacontroller

我需要将触摸事件捕获到MediaController进度滑块,以便MediaPlayer在手指抬离滑块之前不会发生更新.

[也许我的情况很独特:我正在为每个"节目"播放多个"堆栈"的流媒体视频.在上一个堆栈完成之前,不会加载下一个堆栈.滑块需要表示所有堆栈的持续时间,"拇指"的进度需要表示总持续时间.这是很容易通过覆盖完成getBufferPercentage(),getCurrentPosition()以及getDuration()方法MediaPlayerControl]

更有问题的是沿着时间线来回"擦洗"(移动拇指).如果它导致数据源set多次与seekTo每次移动一起,那么事情会很快陷入困境并崩溃.如果MediaPlayer在用户完成擦除之前没有执行任何操作会更好.

正如其他人所写,是的,最好编写我自己的MediaController实现.但为什么要重做所有这些工作呢?我尝试扩展MediaController,但很快就变得复杂了.我只想抓住滑块的触摸事件!

coc*_*oco 15

一个人能够获得MediaController元素的句柄:

final int topContainerId1 = getResources().getIdentifier("mediacontroller_progress", "id", "android");
final SeekBar seekbar = (SeekBar) mController.findViewById(topContainerId1);
Run Code Online (Sandbox Code Playgroud)

然后在搜索栏上设置一个监听器,这将要求您实现其三个公共方法:

seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // Log.i("TAG", "#### onProgressChanged: " + progress);
        // update current position here
        }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // Log.i("TAG", "#### onStartTrackingTouch");
        // this tells the controller to stay visible while user scrubs
        mController.show(3600000);
        // if it is possible, pause the video
        if (playerState == MPState.Started || playerState == MPState.Paused) {
            mediaPlayer.pause();
            playerState = MPState.Paused;
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int seekValue = seekBar.getProgress();
        int newMinutes = Math.round((float)getTotalDuration() * (float)seekValue / (float)seekBar.getMax());
        // Log.i("TAG", "#### onStopTrackingTouch: " + newMinutes);
        mController.show(3000); // = sDefaultTimeout, hide in 3 seconds
    }
});
Run Code Online (Sandbox Code Playgroud)

警告:当您擦洗时,这不会更新当前位置,即左侧TextView时间值.(我有一个非常了不起的方法来做这个,这个边际太小而不能包含).

  • 请注意,SeekBar**的实例化必须发生在`VideoView.onPrepared(..)`Callback方法中. (2认同)