android exoplayer实现左右滑动屏幕快进快退

  • Post author:
  • Post category:其他


我的代码写的比较乱,不好意思拿上来,但是我个人亲测过,还是可以实现快进快退的功能的。截取一部分供大家参考

simpleExoPlayerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        x = (int) event.getRawX();
                        y = (int) event.getRawY();

                        break;
                    case MotionEvent.ACTION_UP:
                        int upX = (int) event.getRawX();
                        int upY = (int) event.getRawY();

                        int disX = upX - x;
                        int dixY = upY - y;
                        if (Math.abs(disX) > Math.abs(dixY) && Math.abs(disX) > 10) {
                            long fastLength = unitLength * disX;
                            long currentLength = (simpleExoPlayer.getCurrentPosition() + fastLength);

                            if (currentLength >= 0 && currentLength <= totalTime) {
                                simpleExoPlayer.seekTo(currentLength);
                            }
                        }

                        break;
                }
                return true;
            }
        });
private long unitLength;
    private long totalTime;
    
    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        switch (playbackState) {
            case PlaybackState.STATE_PLAYING:
                // 获取总时长,然后计算
                if (totalTime == 0) {
                    totalTime = (simpleExoPlayer.getDuration());
                    int width = MobileScreenUtils.getScreenWidth(mContext);
                    if (totalTime < width) {
                        unitLength = (totalTime % width);
                    } else {
                        unitLength = (totalTime / width);
                    }
                    LogUtils.e("总时长是:" + totalTime + "--" + width);
                }
                initPlayVideo();
                break;

            case PlaybackState.STATE_BUFFERING:
                System.out.println("播放状态: 缓存完成 playing");
                break;

            case PlaybackState.STATE_CONNECTING:
                System.out.println("播放状态: 连接 CONNECTING");
                break;

            case PlaybackState.STATE_ERROR://错误
                System.out.println("播放状态: 错误 STATE_ERROR");
                break;

            case PlaybackState.STATE_FAST_FORWARDING:
                System.out.println("播放状态: 快速传递");
                pausePlay();//暂停播放
                break;

            case PlaybackState.STATE_NONE:
                System.out.println("播放状态: 无 STATE_NONE");
                break;

            case PlaybackState.STATE_PAUSED:
                System.out.println("播放状态: 暂停 PAUSED");
                break;

            case PlaybackState.STATE_REWINDING:
                System.out.println("播放状态: 倒回 REWINDING");
                break;

            case PlaybackState.STATE_SKIPPING_TO_NEXT:
                System.out.println("播放状态: 跳到下一个");
                break;

            case PlaybackState.STATE_SKIPPING_TO_PREVIOUS:
                System.out.println("播放状态: 跳到上一个");
                break;

            case PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM:
                System.out.println("播放状态: 跳到指定的Item");
                break;

            case PlaybackState.STATE_STOPPED:
                System.out.println("播放状态: 停止的 STATE_STOPPED");
                break;
        }
    }

这个方法是实现了Player.EventListener接口实现的。unitLength是根据屏幕宽度和视频总时长来计算出来的。



版权声明:本文为u010648159原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。