Create a Video recorder
Description
The following code shows how to Create a Video recorder.
Example
Setup permission in manifest xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.java2s.myapplication3.app" >
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="java2s.com"
android:theme="@style/AppTheme" >
<activity
android:name="com.java2s.myapplication3.app.MainActivity"
android:label="java2s.com" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main layout xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button_record"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="onRecordClick" />
<SurfaceView
android:id="@+id/surface_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/button_record" />
</RelativeLayout>
Main activity Java code
package com.java2s.myapplication3.app;
/* w ww . ja v a 2 s . c o m*/
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private MediaRecorder mRecorder;
private SurfaceView mPreview;
private Button mRecordButton;
private boolean mRecording = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecordButton = (Button) findViewById(R.id.button_record);
mRecordButton.setText("Start Recording");
mPreview = (SurfaceView) findViewById(R.id.surface_video);
mPreview.getHolder().addCallback(this);
mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera = Camera.open();
//Rotate the preview display to match portrait
mCamera.setDisplayOrientation(90);
mRecorder = new MediaRecorder();
}
@Override
protected void onDestroy() {
mCamera.release();
mCamera = null;
super.onDestroy();
}
public void onRecordClick(View v) {
updateRecordingState();
}
/*
* The order of these methods are important.
*/
private void initializeRecorder() throws IllegalStateException, IOException {
mCamera.unlock();
mRecorder.setCamera(mCamera);
mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
File recordOutput = new File(Environment.getExternalStorageDirectory(), "recorded_video.mp4");
if (recordOutput.exists()) {
recordOutput.delete();
}
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mRecorder.setProfile(cpHigh);
mRecorder.setOutputFile(recordOutput.getAbsolutePath());
mRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
mRecorder.setMaxDuration(50000); // 50 seconds
mRecorder.setMaxFileSize(5000000); // Approximately 5 megabytes
mRecorder.prepare();
}
private void updateRecordingState() {
if (mRecording) {
mRecording = false;
mRecorder.stop();
mRecorder.reset();
mCamera.lock();
mRecordButton.setText("Start Recording");
} else {
try {
initializeRecorder();
mRecording = true;
mRecorder.start();
mRecordButton.setText("Stop Recording");
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) { }
@Override
public void surfaceDestroyed(SurfaceHolder holder) { }
}