Program Tip

Android : VideoView에 onClickListener를 제공 할 수없는 이유는 무엇입니까?

programtip 2020. 12. 7. 20:37
반응형

Android : VideoView에 onClickListener를 제공 할 수없는 이유는 무엇입니까?


다음 코드 줄을 작성했습니다.

 mVideoView = (VideoView) findViewById(R.id.video_view);
    mVideoView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("LOG_TAG, click");
        }
    });

그러나 내 응용 프로그램을 실행할 때 클릭 이벤트가 호출되지 않습니다.

그래서 VideoView에 OnClickListener를 등록하는 것이 불가능합니까? 그렇다면 그 이유는 무엇입니까?


사용 VideoView.setOnTouchListener(..)이 VideoView를 지켜 보면서 작동


onTouch를 사용하여 VideoViews의 일시 중지 / 재생을 해결하는 방법은 다음과 같습니다.

// 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;
    }
});

나는 이것이 오래되었다는 것을 알고 있지만 이것을 사용했습니다.

    mVideoView.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            Log.i(TAG, "Video 1 clicked, starting playback");

            return false;
        }
    });

나는 이것이 오래된 질문이라는 것을 알고 있지만 여기에 내가 한 일이 있습니다.

setOnClickListener가 트리거되지 않았기 때문에 VideoView를 확장하는 자체 클래스를 만들었습니다.

public class VideoViewCustom extends VideoView{

onTouchEvent를 재정의했습니다.

@Override
public boolean onTouchEvent(MotionEvent ev) {

    if(ev.getAction() == MotionEvent.ACTION_DOWN){
        Log.d(TAG, "ACTION_DOWN");
    }

    return super.onTouchEvent(ev);
}

이제 MotionEvent로 onClick 이벤트를 가져올 수 있습니다.

이것이 누군가를 돕기를 바랍니다!


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.


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.


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.


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


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!


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();
            }
        }
    });

참고URL : https://stackoverflow.com/questions/6175456/android-why-cant-i-give-an-onclicklistener-to-a-videoview

반응형