Record Video with Android SDK

So.. the other day I wanted to add the ability to record video depositions to one of my mobile public safety apps — something that I thought would be a piece of cake — It turns out there are a few subtleties that can make for a lot of wasted time.

I thought I would share my solution that works well on Android 2.3 and 3.2

I’ll start with a recorder preview class

import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class RecordPreview extends SurfaceView implements SurfaceHolder.Callback {
  // Create objects for MediaRecorder and SurfaceHolder.
  MediaRecorder recorder;
  SurfaceHolder mHolder;
  String videoUploadURL;
  String localVideoFile;

  // Create constructor of Preview Class. In this, get an object of
  // surfaceHolder class by calling getHolder() method. After that add
  // callback to the surfaceHolder. The callback will inform when surface is
  // created/changed/destroyed. Also set surface not to have its own buffers.

  public RecordPreview(Context context, MediaRecorder temprecorder,
      String localVideoFile, String videoUploadURLStr) {
    recorder = temprecorder;
    mHolder = getHolder();
    this.localVideoFile = localVideoFile;
    this.videoUploadURL = videoUploadURLStr;

  // Implement the methods of SurfaceHolder.Callback interface

  // SurfaceCreated : This method gets called when surface is created.
  // In this, initialize all parameters of MediaRecorder object as explained
  // above.

  public void surfaceCreated(SurfaceHolder holder) {
    try {
      // the order of these calls matter -- wasted a lot of time
      recorder.setOutputFile("/sdcard/" + localVideoFile + ".mp4");
    catch (Exception e) {
      String message = e.getMessage();
      Log.e("RecordPreview""SurfaceCreated Error: ", e);

  // SurfaceDestroyed : This method gets called immediately before the
  // surface is being destroyed. Stop camera preview because surface will no
  // longer exist.

  public void surfaceDestroyed(SurfaceHolder holder) {
    if (recorder != null) {
      recorder = null;

  // surfaceChanged : This method is called after the surface is created.
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {


Next is the Video Recorder Activity

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class VideoRecorder extends Activity {

  public static final String EXTRA_VIDEO_NAME = "video_name";
  //Create objects of MediaRecorder and Preview class
  private MediaRecorder recorder;
  private RecordPreview preview;
  private String videoName;
      // In this method, create an object of MediaRecorder class. Create an object of
      // RecorderPreview class(Customized View). Add RecorderPreview class object
      // as content of UI.

  public void onCreate(Bundle savedInstanceState)
      videoName = getIntent().getStringExtra(EXTRA_VIDEO_NAMEthis.hashCode();
      recorder = new MediaRecorder();
      preview = new RecordPreview(this,recorder, videoName, null);

  //Create option menu which can be used for starting and stopping video recording.
  // Also implement the onOptionsItemSelected to handle the events for option menu.

  public boolean onCreateOptionsMenu(Menu menu)
      return super.onCreateOptionsMenu(menu);

  public boolean onOptionsItemSelected(MenuItem item)
      switch (item.getItemId())
          case 0:
          case 1:
      return super.onOptionsItemSelected(item);

Thats it! now just need to start the Activity. Don’t forget to add the correct permissions to your manifest.

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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>