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.
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,
1. Video Recording and Live Preview at Recording
The state transition diagram for Android MediaRecorder class is as below,
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.