How to record SurfaceView preview

老子叫甜甜 提交于 2021-01-29 14:55:52

问题


I am working on an app in which I am using Surface View only for preview of frames. Can anyone tell me how can I record videos of this SurfaceView preview?


回答1:


You have 3 possibilities :

1 - Capture each frame of your SurfaceView and store all the captured bitmaps into an array, after that you can encode it to a video file using MediaRecord

Here's a full example of how it works : ViewRecorder

2 - Using EZFilter (I've already tested), It's a little long but it's worth a try :

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

When you want to start recording :

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

To stop recording :

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

3 - You can capture the whole screen and crop the recorded video using FFmpeg



来源:https://stackoverflow.com/questions/63793136/how-to-record-surfaceview-preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!