gpt4 book ai didi

android - SurfaceView 之上的 VideoView

转载 作者:太空狗 更新时间:2023-10-29 12:57:01 24 4
gpt4 key购买 nike

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

当我转到主屏幕时,我在淡入淡出的动画中看到了一瞬间的 VideoView,所以它确实存在。

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

这是我的 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);
}
}
}

我的 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 的绘图线程获取消息。这就是为什么我实现了一个在使用菜单时发送消息的线程。

最佳答案

根据 this thread,我不认为你可以有重叠的 SurfaceViews Android框架工程师回答:

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.

关于android - SurfaceView 之上的 VideoView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5433525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com