VideoView 位于 SurfaceView 之上

发布于 2024-10-26 11:40:53 字数 4632 浏览 4 评论 0原文

我尝试在 SurfaceView 之上显示 VideoView。但它不可见,但会对点击做出反应(MediaController 出现并播放声音)。该视频似乎在 SurfaceView 后面播放,因此我也尝试使用 setZOrderMediaOverlay() 和/或 setZOrderOnTop() 但没有任何改变

当我进入主屏幕时,我在褪色动画中瞬间看到了 VideoView,所以它确实存在。

我用 xml 布局尝试过,也完全以编程方式尝试过,但没有任何效果。

这是我的活动:

public class VideoTest extends Activity {
    private RelativeLayout mLayout;
    private VideoView mVideo;
    private Handler mHandler;
    private FrameLayout mFrameLayout;
    private SurfaceView mSurfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mFrameLayout = new FrameLayout(this);
        mFrameLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        mLayout = new RelativeLayout(this);
        mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        mVideo = new VideoView(this);
        mVideo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mSurfaceView = new SurfaceView(this);
        mSurfaceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        mSurfaceView.setZOrderMediaOverlay(false);
        mSurfaceView.setZOrderOnTop(false);

        mFrameLayout.addView(mSurfaceView);
        mLayout.addView(mVideo);
        mFrameLayout.addView(mLayout);

        setContentView(mFrameLayout);

        // with xml
//        setContentView(R.layout.main);
//        mFrameLayout = (FrameLayout) findViewById(R.id.activity_reader);
//        mLayout = (RelativeLayout) findViewById(R.id.videoview);
//        mVideo = (VideoView) findViewById(R.id.video_view);
//        mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);

        // Set the handler to me messaged from the threads
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg != null) {
                    mVideo.setMediaController(new MediaController(mVideo.getContext()));
                    mVideo.setVideoPath("/mnt/sdcard/myvideo.mp4");
                    if (mLayout.getVisibility() == View.VISIBLE) {
                        mLayout.setVisibility(View.GONE);
                    } else {
                        mLayout.setVisibility(View.VISIBLE);
                    }
//                    mFrameLayout.bringChildToFront(mLayout);
//                    mFrameLayout.bringChildToFront(mVideo);
//                    mFrameLayout.requestLayout();
//                    mFrameLayout.invalidate();
//                    mFrameLayout.postInvalidate();
//                    mVideo.requestFocus();
                }
                super.handleMessage(msg);
            }
        };
    }

    /** Inflate the menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_reader_menu, menu);
            return true;
    }

    /** Handle menu items events */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.toggle_video:
                new Thread() {
                    public void run() {
                        mHandler.sendEmptyMessage(0);
                    };
                }.start();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_reader"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <RelativeLayout android:id="@+id/videoview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone">
        <VideoView android:id="@+id/video_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</FrameLayout>

一个提示:由于这只是一项测试,以找出它不起作用的原因,因此我尝试最小化真实应用程序中的代码,其中 VideoView 的可见性为由从 SurfaceView 的绘图线程获取消息的处理程序控制。这就是为什么我在使用菜单时实现了一个发送消息的线程的原因。

I try to show a VideoView on top of a SurfaceView. But it isn't visible but reacts on clicks (MediaController appears and the sound plays). The video seems to be played behind the SurfaceView so I also tried to make use of setZOrderMediaOverlay() and/or setZOrderOnTop() but nothing changed

When I go to the home screen I see the VideoView for a split second in the fading animation, so it is really there.

I tried it with a xml layout and also completely programmatically but nothing works.

Here is my activity:

public class VideoTest extends Activity {
    private RelativeLayout mLayout;
    private VideoView mVideo;
    private Handler mHandler;
    private FrameLayout mFrameLayout;
    private SurfaceView mSurfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mFrameLayout = new FrameLayout(this);
        mFrameLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        mLayout = new RelativeLayout(this);
        mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        mVideo = new VideoView(this);
        mVideo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mSurfaceView = new SurfaceView(this);
        mSurfaceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        mSurfaceView.setZOrderMediaOverlay(false);
        mSurfaceView.setZOrderOnTop(false);

        mFrameLayout.addView(mSurfaceView);
        mLayout.addView(mVideo);
        mFrameLayout.addView(mLayout);

        setContentView(mFrameLayout);

        // with xml
//        setContentView(R.layout.main);
//        mFrameLayout = (FrameLayout) findViewById(R.id.activity_reader);
//        mLayout = (RelativeLayout) findViewById(R.id.videoview);
//        mVideo = (VideoView) findViewById(R.id.video_view);
//        mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);

        // Set the handler to me messaged from the threads
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg != null) {
                    mVideo.setMediaController(new MediaController(mVideo.getContext()));
                    mVideo.setVideoPath("/mnt/sdcard/myvideo.mp4");
                    if (mLayout.getVisibility() == View.VISIBLE) {
                        mLayout.setVisibility(View.GONE);
                    } else {
                        mLayout.setVisibility(View.VISIBLE);
                    }
//                    mFrameLayout.bringChildToFront(mLayout);
//                    mFrameLayout.bringChildToFront(mVideo);
//                    mFrameLayout.requestLayout();
//                    mFrameLayout.invalidate();
//                    mFrameLayout.postInvalidate();
//                    mVideo.requestFocus();
                }
                super.handleMessage(msg);
            }
        };
    }

    /** Inflate the menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_reader_menu, menu);
            return true;
    }

    /** Handle menu items events */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.toggle_video:
                new Thread() {
                    public void run() {
                        mHandler.sendEmptyMessage(0);
                    };
                }.start();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

My xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_reader"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <RelativeLayout android:id="@+id/videoview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone">
        <VideoView android:id="@+id/video_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</FrameLayout>

One hint: As this is only a test to find out why it isn't working I tried to minimize the code from my real application where the visibility of the VideoView is controlled by a handler who get a message from the drawing thread of the SurfaceView. Thats the reason why I implemented a thread sending the message when the menu was used.

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

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

发布评论

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

评论(1

帅气尐潴 2024-11-02 11:40:53

根据 此线程<,我认为您不能拥有重叠的 SurfaceViews /a> Android框架工程师的回答:

抱歉,你不能这样做——因为
表面景色很特别,不
真正的观点(表面是一个
单独的窗口按 Z 顺序排列
自己的),它们的 Z 顺序不匹配
你的意见。表面视图很大,
重物;你不打算
将 SurfaceView 像普通视图一样对待
就这样。

I don't think that you can have overlapping SurfaceViews, according to this thread answered by an Android framework engineer:

Sorry, you can't do this -- because
surface views are very special and not
really views (the surface is a
separate window Z-ordered with your
own), their Z-ordering does not match
your views. A surface view is a big,
heavy object; you are not intended to
treat SurfaceView like a regular view
in this way.

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