Preview of camera+surface at the beginning of live video

Keywords: Mobile Android SurfaceView Java

The beginning of live video

Nowadays, there are many live platforms on various networks, but do you know how to do live video? People who don't understand may think it should be similar to video recording. But the implementation of android is quite different.

When recording local video on android, you can use MediaRecord to pass in relevant parameters and write in the recorded address, which is basically ok. But for live video, it is difficult. First, we need to get the video data, then through hardware coding, and finally upload. Let's take a look at the simple implementation steps:

1. First, we need to preview the video. Generally, there are three controls for preview: surfaceview,GLSurfaceView,TextureView. Although there are all differences in the use of these three controls, the data is ultimately retrieved from the surface. Due to the Android version's revision of the camera part, android.hardware.Camera was used before Android 21, and then android.hardware.camera2 was added. There are many combinations to realize it:

a.Camera+Surface

package com.example.amei.cameraexample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;
import java.util.List;

public class camera_surface_Activity extends AppCompatActivity {
   private static final String TAG = "camera_surface_Activity";
   private SurfaceView mSurfaceView = null;
   private Camera mCamera = null;
   private Camera.Parameters mParameters = null;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       checkPermission();
       setContentView(R.layout.activity_camera_surface);
       mSurfaceView = (SurfaceView)findViewById(R.id.surface_id);

       SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
       surfaceHolder.addCallback(new SurfaceHolder.Callback() {
           @Override
           public void surfaceCreated(SurfaceHolder surfaceHolder) {
               Log.i(TAG,"surfaceCreated");
               openCamera();
               try {
                   mCamera.setPreviewDisplay(surfaceHolder);
               } catch (IOException e) {
                   e.printStackTrace();
               }
               mCamera.startPreview();
           }

           @Override
           public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
               Log.i(TAG,"surfaceChanged");


           }

           @Override
           public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
               Log.i(TAG,"surfaceDestroyed");
               if(mCamera != null)
               {
                   mCamera.release();
                   mCamera = null;
               }
           }
       });
   }
   private void openCamera()
   {
       if(mCamera == null){
           mCamera = android.hardware.Camera.open(0);
           mParameters = mCamera.getParameters();
           List<Camera.Size> list = mParameters.getSupportedPreviewSizes();
           mParameters.setPreviewFormat(ImageFormat.NV21);
           int PreviewWidth = list.get(0).width;
           int PreviewHeight = list.get(0).height;
           Log.i(TAG,"PreviewWidth = "+PreviewWidth+"  PreviewHeight = "+PreviewHeight);
           mParameters.setPreviewSize(PreviewWidth, PreviewHeight); // Set preview picture size



           mCamera.setParameters(mParameters);
       }
   }
   private void checkPermission() {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
           String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
           for (String permission : permissions) {
               if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                   ActivityCompat.requestPermissions(this, permissions, 200);
                   return;
               }
           }
       }
   }
}

There is only one surface in the layout:

   <SurfaceView
       android:id="@+id/surface_id"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent" />

First, get the corresponding surface view, and set the callback. You can preview only after the surface is ready, so when the surface is created successfully, open the camera.

Posted by jmcall10 on Sun, 08 Dec 2019 13:05:28 -0800