Android: Why can't I give an onClickListener to a VideoView?

前端 未结 11 2108
独厮守ぢ
独厮守ぢ 2021-02-03 17:08

I have written these lines of code:

 mVideoView = (VideoView) findViewById(R.id.video_view);
    mVideoView.setOnClickListener(new OnClickListener() {
        @O         


        
相关标签:
11条回答
  • 2021-02-03 17:38

    This is probably long overdue, nonetheless of some help to those who may run into a similar problem. The way I got around this problem was by laying a transparent image view right on top of the video view, then listening to onClick events on the image view, and doing whatever it was I wanted to do with the video view afterwards.

    0 讨论(0)
  • 2021-02-03 17:44

    You can solve this issue through cover layout. I used the linearlayout.

                <LinearLayout
                    android:id="@+id/video1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center">
                    <VideoView
                        android:id="@+id/video2"
                        android:layout_width="370dp"
                        android:layout_height="180dp"
                        android:layout_gravity="center"
                        app:elevation = "0dp"
                        android:background="@mipmap/video"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        android:visibility="visible"
                        />
    
                </LinearLayout>
    

    and try it in source file.

    video1.setOnClickListener {
                    if(Video.isPlaying) {
                        Video.pause()
                    }
                    else {
                        Video.start()
                    }
    }
    
    0 讨论(0)
  • 2021-02-03 17:45

    Here's how I solved the pause/play of VideoViews using onTouch:

    // Class variables
    private boolean bVideoIsBeingTouched = false;
    private Handler mHandler = new Handler();
    
    vvVideo.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        if (!bVideoIsBeingTouched) {
            bVideoIsBeingTouched = true;
        if (vvVideo.isPlaying()) {
            vvVideo.pause();
        } else {
            vvVideo.resume();
        }
        mHandler.postDelayed(new Runnable() {
            public void run() {
                bVideoIsBeingTouched = false;
            }
            }, 100);
        }
        return true;
        }
    });
    
    0 讨论(0)
  • 2021-02-03 17:45

    You might try Gesture Overlay View

    You should be able to overlay this view on top of another view in order to get touch events.

    Hope this helps!

    0 讨论(0)
  • 2021-02-03 17:46

    use VideoView.setOnTouchListener(..) it works for VideoView

    0 讨论(0)
提交回复
热议问题