VideoView在SurfaceView顶部 [英] VideoView on top of SurfaceView

查看:267
本文介绍了VideoView在SurfaceView顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图表明一个 VideoView SurfaceView 的顶部。但它是不可见的,但它响应的点击(的MediaController出现,并播放声音)。视频似乎后面将要播放的 SurfaceView 所以我也试图利用 setZOrderMediaOverlay的()和/或 setZOrderOnTop(),但什么都没有改变

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

当我去到主屏幕上我看到了VideoView一瞬间在渐渐消失的动画,所以它是实实在在地存在着。

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

我试过跟一个XML布局也完全编程,但没有任何工程。

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

下面是我的活动:

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:

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>

一个提示:由于这只是一个测试,找出为什么它不工作我试图尽量减少从我的实际应用中code,其中的 VideoView 是谁从 SurfaceView 的绘制线程得到一个消息处理程序进行控制。这就是为什么我实现了一个线程发送时使用菜单中的消息的原因。

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.

推荐答案

我不认为你可以有重叠SurfaceViews,根据的这个线程通过一个Android框架工程师回答:

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

对不起,你不能做到这一点 - 因为   表面的观点是非常特殊的,而不是   真正的意见(表面上是一个   单独的窗口的Z排序,您   自己的),它们的Z排序不匹配   您的意见。从表面上看是一个大的,   重物;你不旨在   治疗SurfaceView像普通视图   以这种方式

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.

这篇关于VideoView在SurfaceView顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆