Вы можете сделать это, чтобы справиться с любым сценарием взаимодействия с игроком, предположим, что вы хотите добавить функции, чтобы пропустить или вернуться через 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 от Юсуфа