ExoPlayer PlayerView OnClickListener не работает - PullRequest
0 голосов
/ 17 сентября 2018

В моем приложении я хочу обработать пользовательский клик по PlayerView, однако OnClickListener не вызывается и не выдается исключение. Я добавил app:use_controller="false" в XML PlayerView.

Я попытался установить OnTouchListener, но мне нужно вызвать MotionEvent.ACTION_UP, что также не происходит (потому что я не хочу обрабатывать случай, когда пользователь выполняет прокрутку, мне нужно обрабатывать случай, когда пользователь нажмите на PlayerView).

  class FeedHolder extends RecyclerView.ViewHolder {
        private TextView displayName, jobTitle, action, date, likedBy;
        private ImageIndicator profile_pic;
        private ImageView videoOptions, likeImage, shareImage;
        private PlayerView playerView;
        private WebView webView;

        @SuppressLint("SetJavaScriptEnabled")
        FeedHolder(View itemView) {
            super(itemView);
            profile_pic = itemView.findViewById(R.id.profile_pic);
            likedBy = itemView.findViewById(R.id.tv_like_by);
            date = itemView.findViewById(R.id.tv_date);
            likeImage = itemView.findViewById(R.id.img_like);
            shareImage = itemView.findViewById(R.id.img_share);
            displayName = itemView.findViewById(R.id.tv_display_name);
            jobTitle = itemView.findViewById(R.id.tv_job_title);
            action = itemView.findViewById(R.id.btn_action);
            videoOptions = itemView.findViewById(R.id.video_options);
            playerView = itemView.findViewById(R.id.exo_player);
            webView = itemView.findViewById(R.id.web_view);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebViewClient(new AnimationWebViewClient());
            webView.getSettings().setAllowFileAccessFromFileURLs(true);
            webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        }
    }


@Override
    public void onBindViewHolder(@NonNull FeedHolder holder, int position) {
      holder.playerView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ...
                }
            });

}

1 Ответ

0 голосов
/ 17 сентября 2018

Вы можете сделать это, чтобы справиться с любым сценарием взаимодействия с игроком, предположим, что вы хотите добавить функции, чтобы пропустить или вернуться через 10 секунд, как youtube.

Это мой фрагмент

@SuppressLint("ClickableViewAccessibility")
    private void setOnGestureListeners() {
        mPlayerView.setOnTouchListener(new OnSwipeTouchListener(requireActivity()){
            @Override
            public void onSwipeRight() {
                super.onSwipeRight();
                // Swipe to the right
            }

            @Override
            public void onSwipeLeft() {
                super.onSwipeLeft();
                // Swipe to the left
            }

            @Override
            public void onClick() {
                super.onClick();
                // User tapped once (This is what you want)
            }

            @Override
            public void onDoubleClick() {
                super.onDoubleClick();
                // User tapped twice
            }
        });
    }

OnSwipeTouchListener.java

public class OnSwipeTouchListener implements View.OnTouchListener {

    private static final String TAG = "OnSwipeTouchListener";

    private final GestureDetectorCompat mDetector;

    public OnSwipeTouchListener(Context context) {
        mDetector = new GestureDetectorCompat(context, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mDetector.onTouchEvent(event);

    }

    public void onSwipeRight() {
        Log.i(TAG, "onSwipeRight: Swiped to the RIGHT");
    }

    public void onSwipeLeft() {
        Log.i(TAG, "onSwipeLeft: Swiped to the LEFT");
    }

    public void onSwipeTop() {
        Log.i(TAG, "onSwipeTop: Swiped to the TOP");
    }

    public void onSwipeBottom() {
        Log.i(TAG, "onSwipeBottom: Swiped to the BOTTOM");
    }

    public void onClick() {
        Log.i(TAG, "onClick: Clicking in the screen");
    }

    public void onDoubleClick() {
        Log.i(TAG, "onClick: Clicking TWO TIMES in the screen");
    }

    public void onLongClick() {
        Log.i(TAG, "onLongClick: LONG click in the screen");
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            onClick();
            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            onDoubleClick();
            return super.onDoubleTap(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            onLongClick();
            super.onLongPress(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                        result = true;
                    }
                } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                    result = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    }
}

Это несколько проектов Exoplayer, если хотите.

UsagesOfExoplayer

ExoPlayerSample от Юсуфа

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...