java - 通过相机预览查看视频

标签 java android video camera

我正在尝试播放视频并想在相机预览中显示它。视频以透明背景播放,视频控件也显示,但看不到视频。我确实听到了正在播放的视频的声音。

这是我的代码:

// 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 顺序。 通过声明两个 View ,我能够在相机预览上覆盖一个 TextView 在 XML 中,并以编程方式将它们覆盖在我的 Activity 的 onCreate 方法上。

假设您有一个带有漂亮透明背景的 TextView 的 XML,您想覆盖在相机预览框架布局上,因为为什么不呢?它看起来很酷!

<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>

如果像这样离开,相机将覆盖您的文字,无论如何 :( 为了解决它,让我们去编程,并且: 1) 将 TextView 从其父 Layout 中分离出来 2) 首先将其连接到相机的框架布局之后

代码如下

OnCreate(){ ...布拉布拉...

//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);

这是执行此操作的一般方法,如果您需要更多详细信息,请告诉我。 我需要在工作中 catch 最后期限,所以我使用第一个想到的解决方案,有时不是最优雅或最有效的,如果您知道更好的方法,请与我们分享!

关于java - 通过相机预览查看视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13196944/

相关文章:

java - Apache TomEE 内存泄漏

java - 是什么导致了 "error: attribute mapbox_styleUrl not found"?

c# - 上传任何视频并在 .net 中在线转换为 .mp4

android - 如何在Android中拦截系统范围的 "copy to clipboard"事件

ios - 如何在运行 session 期间更改 AVCaptureMovieFileOutput 视频方向?

javascript - 具有循环功能的 <video> 元素不会在 Chrome 或 Firefox 中无缝循环播放视频

java - 在Android上通过OkHttp连接到websocket时如何捕获UnknownHostException?

java - 如何在 java 中使用 StringToWordVector (weka)?

java - 如何像维护C#应用程序中的配置文件一样维护java应用程序项目中的配置文件?

android - 使用 Picasso 加载图像时显示模糊