How to make a Video Camera using Android Studio 3.0.1

This program helps how to capture Video with audio using an existing camera application.

There are two tools- Button, SurfaceView

 

MainActivity.java—-

public class MainActivity extends AppCompatActivity
{
    private SurfaceHolder sh;
    private SurfaceView sv;
    public MediaRecorder mrec = new MediaRecorder();
    private Camera camera;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_main);
        try {
            button=(Button) findViewById(R.id.button);
            sv = (SurfaceView) findViewById(R.id.surfaceView);
            camera = Camera.open();
            setTitle("Start");

            sh = sv.getHolder();
            sh.addCallback(new RecorderSurface(camera,this));
            sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
        catch (Exception ex)
        {
            System.out.println(ex);
            setTitle(ex.getMessage());
        }
    }



    public void TakeVideo(View view)
    {
        if(button.getText().equals("Start"))
        {
            try  {

                startRecording();
                button.setText("Stop");

            } catch (Exception e) {

                String message = e.getMessage();
                setTitle("Problem " + message);
                mrec.release();
            }

        }
        else if(button.getText().equals("Stop"))
        {
            mrec.stop();
            mrec.release();
            mrec = null;
            button.setText ("Start");
        }


    }

    protected void startRecording() throws IOException
    {
        if(camera==null)
            camera = Camera.open();

        String filename;
        String path;

        path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath().toString();

        Date date=new Date();
        filename="/pappu"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";

        //create empty file it must use
        File file=new File(path,filename);

        mrec = new MediaRecorder();

        camera.lock();
        camera.unlock();
        // mCamera.setDisplayOrientation(90); // use for set the orientation of the preview
        mrec.setOrientationHint(90);
        mrec.setCamera(camera);
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
        mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mrec.setPreviewDisplay(sh.getSurface());
        mrec.setOutputFile(path+filename);
        mrec.prepare();
        mrec.start();


    }

    protected void stopRecording() {

        if(mrec!=null)
        {
            mrec.stop();
            mrec.release();
            camera.release();
            camera.lock();
        }
    }

    private void releaseMediaRecorder() {

        if (mrec != null) {
            mrec.reset(); // clear recorder configuration
            mrec.release(); // release the recorder object
        }
    }

    private void releaseCamera() {
        if (camera != null) {
            camera.release(); // release the camera for other applications
            camera = null;
        }

    }
}

We have to make a new class named “RecorderSurface”—-

public class RecorderSurface  implements SurfaceHolder.Callback {

    private Camera camera;
    private Context context;
    public RecorderSurface(Camera camera, Context context)
    {
        this.camera=camera;
        this.context=context;
    }
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        if (camera != null) {
            Camera.Parameters params = camera.getParameters();
            camera.setParameters(params);
            Toast.makeText(context, "Surface Created", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(context, "Camera is not available",
                    Toast.LENGTH_LONG).show();


        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        camera.stopPreview();
        camera.release();
    }
}

We have to give permission in manifests—

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

activity_main.xml——-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.pradeepkumar.videocamerademo.MainActivity">


    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/button"
        android:layout_centerInParent="true"
        android:layout_weight="1"/>

    <Button
        android:background="#22FF00"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:onClick="TakeVideo"
        android:text="Start" />
</RelativeLayout>

Leave a Reply