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

前端 未结 11 2107
独厮守ぢ
独厮守ぢ 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:24

    I know this is and old question, but here is what I did:

    Since setOnClickListener is not been triggered, I created my own class which extends VideoView

    public class VideoViewCustom extends VideoView{
    

    and Overrided the onTouchEvent

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    
        if(ev.getAction() == MotionEvent.ACTION_DOWN){
            Log.d(TAG, "ACTION_DOWN");
        }
    
        return super.onTouchEvent(ev);
    }
    

    and now I can get the onClick event with the MotionEvent.

    Hope this helps someone!

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

    You can well use a button which is transparent on the video view if you want a specific part of the video on touch to do something.

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

    It is possible I have did this with onSetClickListener. And Here's a code for your help:

    mVideoView.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Here you put a condition for playing a video when you click on your video view.//
                if(my_video.isPressed())
                {   
                    my_video.start();
                } else {
                    Toast.makeText(getApplicationContext(), 
                        "Not able to run This Video!", 
                        Toast.LENGTH_LONG).show();
                }
            }
        });
    
    0 讨论(0)
  • 2021-02-03 17:27

    I know this is old but I used this:

        mVideoView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
                Log.i(TAG, "Video 1 clicked, starting playback");
    
                return false;
            }
        });
    
    0 讨论(0)
  • 2021-02-03 17:27

    I realize this is an old question but thought I'd chime in with an easy workaround. I can't answer why this doesn't work - seems like a big oversight in my opinion. But an easy workaround is to place your VideoView as the sole View inside a FrameLayout, and set an OnClickListener on the layout. Not ideal, but it works.

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

    The VideoView is a wrapper containing mdeiaplayer and surfaceView. You can interact with through MediaController or writing your own SurfaceView and implement onClick events.

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