Android Video Recording API–Illustrated with an Example

Android has made video recording very simply by providing a few high level classes, including Camera, SurfaceView and MediaRecorder. This tutorial illustrates the Video Recording API by providing a workable example as shown in the screenshots.

cam1cam2

Figure 1. Screenshots of Android Video Capturing Sample Program

The Camera class allows us to access the Camera on an Android device; the SurfaceView class provides a drawing surface, it is used to present a live preview in our application; the MediaRecoder contains the API to configure and record the video, it uses Camera class to access the hardware camera.

0. Live Preview Before Recording

Before users start to record video, they want to see what they’re capturing. That’s what the preview function does, it allows one to see what the camera is able to capture. The preview implementation is briefly illustrated as the code below,

SurfaceView prSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);

SurfaceHolder prSurfaceHolder = prSurfaceView.getHolder();

prSurfaceHolder.addCallback(this);

prSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Camera prCamera = Camera.open();        

Camera.Parameters lParam = prCamera.getParameters();

prCamera.setParameters(lParam);

prCamera.setPreviewDisplay(prSurfaceHolder);

prCamera.startPreview();

prCamera.stopPreview();

1. Video Recording and Live Preview at Recording

The state transition diagram for Android MediaRecorder class is as below,

image

Figure 2. State Transition of Android MediaRecorder

The implementation follows the state transition diagram closely. Starting from Initial state, our code sets the audio source as microphone, video source as camera, and the program enters Initialized state. The output format can be set to mpeg4 or 3gp, which indicates the container format for the video recorded. At DataSourceConfigured state, we set the audio encoder as AMR_NB (Later versions of Android supports AAC), and video encoder as MPEG4,H263 or H264 based on user configurations. We also set the frame rate, and output file here. After that, we call prepare method to transit to Prepared state, and finally a start method call will start the recording.

The description above doesn’t contain every detail but just for illustration. The actual code in feipeng.yacamcorder.Main.java contains lots of error handling and code for remembering and setting user configurations.

Note that the encoders a device supports may differ.

2. Download

You can download the entire source code for the example above from here, or from my github repo.

References:

1. http://developer.android.com/guide/appendix/media-formats.html

2. http://developer.android.com/guide/topics/media/camera.html

0 thoughts on “Android Video Recording API–Illustrated with an Example”

  1. hi,
    please tell me how to send this video to server side and record in a file on server side.I found

    ParcelFileDescriptor fromSocket to send video to server, but i don’t know how to receive this on server side in a file

    Is it possible??? please inform me, this my email address umer_rock_12@hotmail.com

  2. Hi,
    Please please help me. i have found a method for android video camera recording to pc but i m stuck to create a file on server side.
    this methods are

    1) Mediarecorder
    parcelFileDescriptor fromSocket
    that perfect to record video on sd card but when i send it to socket it doesn’t receive on server side not create a video file.

    Thanks and Regards,

    Umer

    1. Sorry, I cannot help you do programming. You’ll need to figure it out yourself. I’m pretty sure there’re resources available online to guide you.

  3. I downloaded the source of this tutorial, when I tried to run it I obtained the following Android Lint error:

    Errors
    call requires api level 8 (current minimum is 6:)android.hardware.Camera#reconnect from Min.java line 209

    Could you please suggest a how to fix this error

    Thanks for the tutorial

    Cheers

    JR

    1. I shut the SDK down and started all over again, and that seemed to have fixed the problem.

      However my system crashes anytime I attempt to record a video. I am using a Samsung galaxy PT-1000 tablet

  4. I am trying to do the same. Did you test this code with API 8 to API 11?
    I cannot get the list of supported video resolutions for the devices running versions prior to API 11. Did you find any solution for this?

  5. hey sorry to say ;i copied and installed the apk file in my android ,n when i press start it is showing “Unfortunately,YACamcorder has stoped . ” error .Anyway it is previewing the screen little upadation in recording part will correct this error…..

Leave a Reply

Your email address will not be published. Required fields are marked *