Video Capture : Video « Media « Android






Video Capture

  
package app.test;

import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;

public class Test extends Activity implements OnClickListener,
    SurfaceHolder.Callback {
  MediaRecorder recorder;
  SurfaceHolder holder;

  boolean recording = false;
  public static final String TAG = "VIDEOCAPTURE";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    recorder = new MediaRecorder();
    initRecorder();
    setContentView(R.layout.main);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    cameraView.setClickable(true);
    cameraView.setOnClickListener(this);
  }

  private void initRecorder() {
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

    CamcorderProfile cpHigh = CamcorderProfile
        .get(CamcorderProfile.QUALITY_HIGH);
    recorder.setProfile(cpHigh);
    recorder.setOutputFile("/sdcard/videocapture.mp4");
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
  }

  private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());

    try {
      recorder.prepare();
    } catch (IllegalStateException e) {
      e.printStackTrace();
      finish();
    } catch (IOException e) {
      e.printStackTrace();
      finish();
    }
  }

  public void onClick(View v) {
    if (recording) {
      recorder.stop();
      recorder.release();
      recording = false;
      Log.v(TAG, "Recording Stopped");
      initRecorder();
      prepareRecorder();
    } else {
      recording = true;
      recorder.start();
    }
  }

  public void surfaceCreated(SurfaceHolder holder) {
    prepareRecorder();
  }

  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
      recorder.stop();
      recording = false;
    }
    recorder.release();
    finish();
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<SurfaceView android:id="@+id/CameraView" android:layout_width="640px" android:layout_height="480px"></SurfaceView>
</LinearLayout>

   
    
  








Related examples in the same category

1.Record video
2.Capture Video
3.Store image and video
4.Load video file from local file system
5.Uri for local video file
6.Custom Video Player
7.Get Video size
8.Display video with VideoView
9.Using MediaController to control Video
10.Play video from Youtube.com
11.Video Gallery
12.View Video with VideoView
13.Video Capture with Metadata
14.VideoView Demo