问题
In my app, I want to handle user click on PlayerView
however OnClickListener
not being called and no exception is thrown. I added app:use_controller="false"
to XML PlayerView
.
I tried to set OnTouchListener
but I need MotionEvent.ACTION_UP
to be called which is also not happening(because I don't want to handle the case when the user is scrolling, I need to handle the case when the user taps on 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:
You can use:
video.setControllerAutoShow(false);
video.setUseController(false);
video.getVideoSurfaceView().setOnClickListener(view -> {
Log.e("GET", "clicked!");
if(player.getPlayWhenReady()){
player.setPlayWhenReady(false);
}else{
player.setPlayWhenReady(true);
}
});
回答2:
You may want to do this to handle any scenario of interaction with the player, suppose you want to add features to skip or come back 10 seconds like youtube.
This is my fragment
@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;
}
}
}
These are some Exoplayer projects, in case you want.
UsagesOfExoplayer
ExoPlayerSample by Yusuf
来源:https://stackoverflow.com/questions/52365953/exoplayer-playerview-onclicklistener-not-working