18 Jun

Video Recording On Android

public class VideoCapture extends Activity 
	implements OnClickListener, 
	SurfaceHolder.Callback {    
     MediaRecorder recorder;
     SurfaceHolder holder;
     boolean recording = false;

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();
}
private void prepareRecorder() {  recorder.setPreviewDisplay(holder.getSurface());    try {        recorder.prepare();    } catch (IllegalStateException e) {        e.printStackTrace();        finish();    } catch (IOException e) {        e.printStackTrace();        finish();    }}
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_example.mp4");
	recorder.setMaxDuration(50000); // 50 s
         recorder.setMaxFileSize(5000000); // 5 mb
}
public void onClick(View v) {    
        if (recording) {        
              recorder.stop();        
              recording = false;

	     // selalu panggil init untuk record ulang        
              initRecorder();        
               prepareRecorder();    
        } else {        
             recording = true;        
             recorder.start();    
        }
}

Leave a Reply

Your email address will not be published. Required fields are marked *