[英]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.