[英]Video view over camera preview
我正在尝试播放视频,并想在相机预览中显示它。 视频在透明背景下播放,还显示了视频控件,但看不到视频。 我确实听到了正在播放视频的声音。
这是我的代码:
// CameraSampleActivity.java, OnCreate Method
View layout_Initialization= (View)findViewById(R.id.layout_Initialization);
layout_Initialization.setVisibility(View.VISIBLE);
View custom_Video_Dialog=(View)findViewById(R.id.videoLayout);
custom_Video_Dialog.setVisibility(View.GONE);
//Code related to camera and camera preview here in the OnCreate method
// CameraSampleActivity.java This code is executed when video needs to be played
View custom_Video_Dialog=(View)findViewById(R.id.videoLayout);
custom_Video_Dialog.setVisibility(View.VISIBLE);
VideoView mVideoView = (VideoView) findViewById(R.id.myvideoview);
mVideoView.setMediaController(new MediaController(CameraSampleActivity.this));
mVideoView.setVideoURI(Uri.parse(firstVideo.getUrl()));
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
mVideoView.setFocusable(true);
mVideoView.requestFocus();
mVideoView.start();
}
});
//这是main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/transparent"
android:orientation="vertical" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<FrameLayout android:layout_width="fill_parent"
android:layout_height="0px" android:layout_weight="1">
<FrameLayout android:id="@+id/cameraPreview"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
<RelativeLayout android:id="@+id/layout_Initialization"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/transparent" android:orientation="horizontal">
.................................
.................................
....................... .........
</RelativeLayout>
<RelativeLayout
android:id="@+id/videoLayout"
android:layout_width="fill_parent"
android:layout_height="280dp" >
<VideoView android:id="@+id/myvideoview"
android:layout_width="fill_parent" android:layout_height="180dp"
android:layout_alignParentTop="true" />
<Button android:id="@+id/videoListButton" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:text="List" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
我认为传递给mediaController的上下文不正确。 请注意,如果我将videoLayout放在frameLayout之外,则视频可见但不透明。 将其放置在Framelayout中可以使我保持透明,并在背面显示摄像机预览的情况下显示视频,这正是我想要的。 有什么帮助吗?
我设法做了一些非常相似的事情,将TextView覆盖在摄像机视频预览上,这可能对您有帮助:
与线性布局不同,声明的顺序未在相对布局中定义Z顺序。 通过以XML声明两个视图,并以编程方式将其视图覆盖在Activity的onCreate方法上,我能够在Camera Preview上覆盖文本视图。
假设您有一个带有TextView的XML,该XML具有很好的透明背景,并且您希望将其覆盖在Camera Preview框架布局上,因为为什么不呢?它看起来很酷!
<RelativeLayout>
<TextView
android:id="@+id/txtnombotiga"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ola ke ase"
android:textColor="#000000"
android:textSize="16sp"
android:background="#55ffffff"
/>
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="240dp">
</FrameLayout>
</RelativeLayout>
如果不这样,摄像机将覆盖你的文字,不管是什么:(要解决这个问题,let's去programatical,并且:1)分离从其父布局2 TextView的) 之后连接到camera's框架布局首先安装相机。
继承人代码
OnCreate(){... blaa blaaa ...
//Create camera instance and reference to our frame Layout
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
//Add camera to the Frame Layout
preview.addView(mPreview);
//Get reference to the TextView you want to overlay and type something funny
TextView txt=(TextView)findViewById(R.id.txtnombotiga);
txt.setText("ola keee aseee!");
//1) Step 1, here comes the magic! dettach the textView from its original Layout
((ViewGroup)txt.getParent()).removeView(txt);
//1) Step 2, voila! attach it to the View, order matters here so it will appear on
//top of the camera!
preview.addView(txt);
这是一般的做法,如果您需要更多详细信息,请告诉我。 我需要按时完成工作,因此我会使用第一个想到的解决方案,有时不是最优雅或最有效的解决方案,如果您知道更好的解决方法,请与我们分享!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.