Implementing Video Recording in Android with MediaRecorder
Incorrect ordering of MediaRecorder configuration methods can lead too initialization failures. The following example demonstrates proper setup for video recording in a Android application.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videorecorder"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".VideoCaptureActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Layout File (activity_video_capture.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<Button
android:id="@+id/btn_start_recording"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="beginRecording"
android:text="Start Recording" />
<Button
android:id="@+id/btn_stop_recording"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="endRecording"
android:text="Stop Recording" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="launchSystemRecorder"
android:text="Use System Camera" />
<SurfaceView
android:id="@+id/surface_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activity File (VideoCaptureActivity.java)
package com.example.videorecorder;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;
public class VideoCaptureActivity extends Activity {
private MediaRecorder videoRecorder;
private SurfaceView previewSurface;
private SurfaceHolder surfaceHolder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_capture);
previewSurface = (SurfaceView) findViewById(R.id.surface_preview);
surfaceHolder = previewSurface.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void beginRecording(View view) {
try {
videoRecorder = new MediaRecorder();
videoRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
videoRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
videoRecorder.setOutputFile("/sdcard/video.3gp");
videoRecorder.setVideoSize(640, 480);
videoRecorder.setVideoFrameRate(5);
videoRecorder.setPreviewDisplay(surfaceHolder.getSurface());
videoRecorder.prepare();
videoRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void endRecording(View view) {
if (videoRecorder != null) {
videoRecorder.stop();
videoRecorder.release();
videoRecorder = null;
}
}
public void launchSystemRecorder(View view) {
Intent systemIntent = new Intent();
systemIntent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
systemIntent.addCategory(Intent.CATEGORY_DEFAULT);
File outputFile = new File("/sdcard/system_record_" + System.currentTimeMillis() + ".3gp");
systemIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFile));
startActivityForResult(systemIntent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "Recording completed", Toast.LENGTH_SHORT).show();
}
}