如何记录SurfaceView预览

问题描述

我正在开发一个仅将Surface View用于帧预览的应用程序。 谁能告诉我如何录制此SurfaceView预览的视频?

解决方法

您有3种可能:

1-捕获SurfaceView的每一帧并将所有捕获的位图存储到数组中,然后您可以使用MediaRecord

将其编码为视频文件

这是其工作方式的完整示例:ViewRecorder

2-使用EZFilter (我已经测试过),虽然有点长,但是值得尝试:

XML:

<RelativeLayout
            android:id="@+id/frm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:animateLayoutChanges="true"
            android:gravity="center_vertical|center_horizontal">


            <cn.ezandroid.ezfilter.core.environment.TextureFitView
                android:id="@+id/render_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:visibility="gone" />

            <cn.ezandroid.ezfilter.view.glview.GLLinearLayout
                android:id="@+id/gl_layout"
                android:layout_width="wrap_content"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:background="@android:color/transparent"
                android:visibility="invisible">

                <!--PUT YOUR SURFACEVIEW XML HERE-->
    
            </cn.ezandroid.ezfilter.view.glview.GLLinearLayout>
        </RelativeLayout>

JAVA:

GLLinearLayout mLinearLayout = findViewById(R.id.gl_layout);
ISupportRecord mSupportRecord;
TextureFitView renderView;
RenderPipeline mRenderPipeline = new RenderPipeline();

mRenderPipeline.setRenderSize((int) surfaceWidth,(int) surfaceHeight);
                mRenderPipeline = EZFilter.input(mLinearLayout)
                        .addFilter(null)
                        .enableRecord(videoPath,true,false)
                        .into(renderView);
                for (GLRender render : mRenderPipeline.getEndPointRenders()) {
                    if (render instanceof ISupportRecord) {
                        mSupportRecord = (ISupportRecord) render;
                    }
                }
                mSupportRecord.setRecordSize(surfaceWidth,surfaceHeight);

要开始录制时:

 private void startRecording() {
        this.mSupportRecord.startRecording();
        this.mSupportRecord.enableRecordAudio(false);
 }

要停止录制:

  private void stopRecording(){
       if (mSupportRecord != null) 
            mSupportRecord.stopRecording();
  }

3-您可以使用FFmpeg

捕获整个屏幕并裁剪录制的视频