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.media.MediaRecorder;
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) {
    super(context);
    recorder = temprecorder;
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    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.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
      recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
      recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
      recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
      recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
      recorder.setOutputFile("/sdcard/" + localVideoFile + ".mp4");
      recorder.setPreviewDisplay(mHolder.getSurface());
      recorder.prepare();
    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) {
    Log.d("RecordPreview""surfaceDestroyed-------------------");
    if (recorder != null) {
      recorder.release();
      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.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
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)
  {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      videoName = getIntent().getStringExtra(EXTRA_VIDEO_NAMEthis.hashCode();
      recorder = new MediaRecorder();
      preview = new RecordPreview(this,recorder, videoName, null);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      setContentView(preview);
  }

  //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)
  {
      menu.add(000"StartRecording");
      menu.add(010"StopRecording");
      return super.onCreateOptionsMenu(menu);
  }

  public boolean onOptionsItemSelected(MenuItem item)
  {
      switch (item.getItemId())
      {
          case 0:
              recorder.start();
              break;
          case 1:
       recorder.stop();
       break;
      }
      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>