Android:为什么我不能为 VideoView 提供 onClickListener?

发布于 2024-11-10 14:29:01 字数 373 浏览 4 评论 0原文

我已经编写了这些代码行:

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

但是,当我运行我的应用程序时,永远不会调用单击事件。

所以我想知道,是否不可能在 VideoView 上注册 OnClickListener ?如果是这样,为什么会这样?

I have written these lines of code:

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

However, when I run my application, the click event is never called.

So I wonder, is it impossible to register an OnClickListener on a VideoView? And, if so, why is that the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

┾廆蒐ゝ 2024-11-17 14:29:01

使用 VideoView.setOnTouchListener(..) 它适用于 VideoView

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

故乡的云 2024-11-17 14:29:01

以下是我如何使用 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;
    }
});

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;
    }
});
标点 2024-11-17 14:29:01

我知道这很旧,但我用过这个:

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

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

            return false;
        }
    });

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;
        }
    });
阳光下慵懒的猫 2024-11-17 14:29:01

我知道这是一个老问题,但这是我所做的:

由于 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 事件。

希望这对某人有帮助!

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!

向日葵 2024-11-17 14:29:01

这可能早就该发生了,尽管如此,对于那些可能遇到类似问题的人来说还是有一些帮助的。我解决这个问题的方法是在视频视图的顶部放置一个透明的图像视图,然后监听图像视图上的 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.

九局 2024-11-17 14:29:01

我意识到这是一个老问题,但我想我会提出一个简单的解决方法。我无法回答为什么这不起作用 - 在我看来这似乎是一个很大的疏忽。但一个简单的解决方法是将 VideoView 作为 FrameLayout 内的唯一 View,并在布局上设置 OnClickListener。不理想,但它有效。

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.

梦魇绽荼蘼 2024-11-17 14:29:01

如果您希望通过触摸视频的特定部分来执行某些操作,您可以使用在视频视图上透明的按钮。

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.

吃兔兔 2024-11-17 14:29:01

您可以通过封面布局来解决这个问题。
我使用了线性布局。

            <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>

并在源文件中尝试。

video1.setOnClickListener {
                if(Video.isPlaying) {
                    Video.pause()
                }
                else {
                    Video.start()
                }
}

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()
                }
}
画尸师 2024-11-17 14:29:01

VideoView 是包含 MediaPlayer 和 SurfaceView 的包装器。您可以通过 MediaController 进行交互或编写自己的 SurfaceView 并实现 onClick 事件。

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

你曾走过我的故事 2024-11-17 14:29:01

您可以尝试 手势叠加视图

您应该能够将此视图叠加到另一个视图的顶部以获得触摸事件。

希望这有帮助!

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!

放肆 2024-11-17 14:29:01

我可能已经用 onSetClickListener 做到了这一点。这是为您提供帮助的代码:

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

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();
            }
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文