在我的应用中,我想处理用户对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) {
...
}
});
}
答案 0 :(得分:4)
您可以使用:
video.setControllerAutoShow(false);
video.setUseController(false);
video.getVideoSurfaceView().setOnClickListener(view -> {
Log.e("GET", "clicked!");
if(player.getPlayWhenReady()){
player.setPlayWhenReady(false);
}else{
player.setPlayWhenReady(true);
}
});
答案 1 :(得分:1)
您可能想要执行此操作以处理与播放器互动的任何情况,假设您想添加功能来跳过或像youtube一样返回10秒。
这是我的片段
@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项目。
答案 2 :(得分:0)
我想这个解决方案会起作用。
playerView.hideController();
playerView.setControllerVisibilityListener(new PlayerControlView.VisibilityListener() {
@Override
public void onVisibilityChange(int visibility) {
if (visibility == View.VISIBLE) {
playerView.hideController();
}
}
});