Here is complete example of how to record video in android device.
(Note: test project in real device not in emulator)
import java.io.File;
import java.io.IOException;
import java.util.Date;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Menu;
import android.view.SurfaceView;
import android.widget.Toast;
public class VideoViewActivity extends Activity implements Callback {
@Override
protected void onDestroy() {
stopRecording();
super.onDestroy();
}
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Camera mCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mCamera = Camera.open();
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, "Start");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getTitle().equals("Start"))
{
try {
startRecording();
item.setTitle("Stop");
} catch (Exception e) {
String message = e.getMessage();
Log.i(null, "Problem " + message);
mrec.release();
}
}
else if(item.getTitle().equals("Stop"))
{
mrec.stop();
mrec.release();
mrec = null;
item.setTitle("Start");
}
return super.onOptionsItemSelected(item);
}
protected void startRecording() throws IOException
{
if(mCamera==null)
mCamera = Camera.open();
String filename;
String path;
path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();
Date date=new Date();
filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";
//create empty file it must use
File file=new File(path,filename);
mrec = new MediaRecorder();
mCamera.lock();
mCamera.unlock();
// Please maintain sequence of following code.
// If you change sequence it will not work.
mrec.setCamera(mCamera);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile(path+filename);
mrec.prepare();
mrec.start();
}
protected void stopRecording() {
if(mrec!=null)
{
mrec.stop();
mrec.release();
mCamera.release();
mCamera.lock();
}
}
private void releaseMediaRecorder() {
if (mrec != null) {
mrec.reset(); // clear recorder configuration
mrec.release(); // release the recorder object
}
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera != null) {
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
Log.i("Surface", "Created");
}
else {
Toast.makeText(getApplicationContext(), "Camera not available!",
Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
}
<SurfaceView android:id="@+id/surface_camera" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
>
</SurfaceView>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
(Note: test project in real device not in emulator)
1. Video View Activity
import java.io.File;
import java.io.IOException;
import java.util.Date;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Menu;
import android.view.SurfaceView;
import android.widget.Toast;
public class VideoViewActivity extends Activity implements Callback {
@Override
protected void onDestroy() {
stopRecording();
super.onDestroy();
}
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Camera mCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mCamera = Camera.open();
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, "Start");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getTitle().equals("Start"))
{
try {
startRecording();
item.setTitle("Stop");
} catch (Exception e) {
String message = e.getMessage();
Log.i(null, "Problem " + message);
mrec.release();
}
}
else if(item.getTitle().equals("Stop"))
{
mrec.stop();
mrec.release();
mrec = null;
item.setTitle("Start");
}
return super.onOptionsItemSelected(item);
}
protected void startRecording() throws IOException
{
if(mCamera==null)
mCamera = Camera.open();
String filename;
String path;
path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();
Date date=new Date();
filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";
//create empty file it must use
File file=new File(path,filename);
mrec = new MediaRecorder();
mCamera.lock();
mCamera.unlock();
// Please maintain sequence of following code.
// If you change sequence it will not work.
mrec.setCamera(mCamera);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile(path+filename);
mrec.prepare();
mrec.start();
}
protected void stopRecording() {
if(mrec!=null)
{
mrec.stop();
mrec.release();
mCamera.release();
mCamera.lock();
}
}
private void releaseMediaRecorder() {
if (mrec != null) {
mrec.reset(); // clear recorder configuration
mrec.release(); // release the recorder object
}
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera != null) {
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
Log.i("Surface", "Created");
}
else {
Toast.makeText(getApplicationContext(), "Camera not available!",
Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
}
2. main.xml file
<SurfaceView android:id="@+id/surface_camera" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
>
</SurfaceView>
3. Get following permission in AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Great example Chirag
ReplyDeletei hv question
how to set mediarecorder for USB TvTuner device???
can u help with example
thanx a million
I would simply say that this is the best thing that has ever happened to me in my Android life so far :D
ReplyDeleteTHANK YOU.
Keep up the good work ;)
This comment has been removed by the author.
ReplyDeletenice but i am unable to run on my mobile, can you please help me in this situation.my mobile model is samsung galaxy gts-5302
ReplyDeletenice code... thank you
ReplyDeleteThanks man !!
ReplyDeleteThanks...
ReplyDeletewow.. worked great. sound is low but it's good. facing one problem on rotating screen it's crashing, any suggestion, how can I solve it? thanks :)
ReplyDeleteit is not working for me plz help me
Deletenice post, but the screen is 90 degree inverted left during recording video.
ReplyDeletecan u solve it
thank u
camera is not opening
ReplyDeletegetting a black screen with just title plz help
ReplyDeleteme too cant. I see black screen (tested on Galaxy s2)
ReplyDeleteNot workin for me
ReplyDeleteHey . Grate tutorial very helpful.
ReplyDeletewaste of time
ReplyDelete