Video recording in Android

public class Video extends Activity implements SurfaceHolder.Callback
{
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
File video;
private Camera mCamera;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_surface);
Log.i(null , “Video starting”);
mCamera = Camera.open();
surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, “StartRecording”);
menu.add(0, 1, 0, “StopRecording”);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case 0:
try {
startRecording();
} catch (Exception e) {
String message = e.getMessage();
Log.i(null, “Problem Start”+message);
mrec.release();
}
break;

case 1: //GoToAllNotes
mrec.stop();
mrec.release();
mrec = null;
break;

default:
break;
}
return super.onOptionsItemSelected(item);
}

protected void startRecording() throws IOException
{
mrec = new MediaRecorder(); // Works well
mCamera.unlock();

mrec.setCamera(mCamera);

mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);

mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile(“/sdcard/zzzz.3gp”);

mrec.prepare();
mrec.start();
}

protected void stopRecording() {
mrec.stop();
mrec.release();
mCamera.release();
}

private void releaseMediaRecorder(){
if (mrec != null) {
mrec.reset(); // clear recorder configuration
mrec.release(); // release the recorder object
mrec = null;
mCamera.lock(); // lock camera for later use
}
}

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);
}
else {
Toast.makeText(getApplicationContext(), “Camera not available!”, Toast.LENGTH_LONG).show();
finish();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
}
camera_surface.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<SurfaceView
android:id=”@+id/surface_camera”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_weight=”1″ />

</RelativeLayout>

And of cource include these permission in manifest.

<uses-permission android:name=”android.permission.RECORD_AUDIO” />
<uses-permission android:name=”android.permission.CAMERA” />
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

Advertisements

One thought on “Video recording in Android

  1. persianas em curitiba March 29, 2013 at 1:44 pm Reply

    Hi there, simply turned into aware of your blog through Google, and found that it is truly informative. I’m gonna watch out for brussels. I will appreciate in the event you continue this in future. A lot of other people will likely be benefited out of your writing. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: