Wednesday, October 26, 2011

Video Capture using MediaRecorder with Flash Light

During video recording using MediaRecorder, we can control the build-in flash light via Camera.setParameters().

Video Capture using MediaRecorder with Flash Light

Modify from the article "A simple exercise of Video Capture using MediaRecorder, for Android 2.3 or higher":

package com.exercise.AndroidVideoCapture;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.Toast;

public class AndroidVideoCapture extends Activity{

private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;

Button myButton;
RadioButton flashOff, flashTorch;
SurfaceHolder surfaceHolder;
boolean recording;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

recording = false;

setContentView(R.layout.main);

//Get Camera for preview
myCamera = getCameraInstance();
if(myCamera == null){
Toast.makeText(AndroidVideoCapture.this,
"Fail to get Camera",
Toast.LENGTH_LONG).show();
}

myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);

myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);

flashOff = (RadioButton)findViewById(R.id.flashoff);
flashTorch = (RadioButton)findViewById(R.id.flashtorch);
}

Button.OnTouchListener flashButtonOnTouchListener
= new Button.OnTouchListener(){

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub

if(myCamera != null){
Parameters parameters = myCamera.getParameters();

switch (arg1.getAction()){
case MotionEvent.ACTION_DOWN:
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
myCamera.setParameters(parameters);
break;
case MotionEvent.ACTION_UP:
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
myCamera.setParameters(parameters);
break;
};
}

return true;
}};

Button.OnClickListener flashModeButtonOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
};

Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(recording){
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object

//Exit after saved
finish();
}else{

//Release Camera before MediaRecorder start
releaseCamera();

if(!prepareMediaRecorder()){
Toast.makeText(AndroidVideoCapture.this,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
finish();
}

mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}};

private Camera getCameraInstance(){
// TODO Auto-generated method stub
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}


private String getFlashModeSetting(){
if(flashTorch.isChecked()){
return Parameters.FLASH_MODE_TORCH;
}else {
return Parameters.FLASH_MODE_OFF;
}
}

private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();

Parameters parameters = myCamera.getParameters();
parameters.setFlashMode(getFlashModeSetting());
myCamera.setParameters(parameters);

mediaRecorder = new MediaRecorder();

myCamera.unlock();
mediaRecorder.setCamera(myCamera);

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M

mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());

try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;

}

@Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}

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

private void releaseCamera(){
if (myCamera != null){
myCamera.release(); // release the camera for other applications
myCamera = null;
}
}

public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

private SurfaceHolder mHolder;
private Camera mCamera;

public MyCameraSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int weight,
int height) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.

if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}

// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}

// make any resize, rotate or reformatting changes here

// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e){
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}
}
}


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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/videoview"
android:layout_width="720px"
android:layout_height="480px"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="REC"
android:textSize="12dp"/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/flashoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OFF"
android:textSize="8dp"
android:checked="true"/>
<RadioButton
android:id="@+id/flashtorch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Torch"
android:textSize="8dp"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
</LinearLayout>


Refer to the article "A simple exercise of Video Capture using MediaRecorder, for Android 2.3 or higher" for AndroidManifest.xml.

Download the files.

5 comments:

chilltime said...

Works fine, this is the solution i had too. I noticed that there is something funny with using media recorder especially on samsung Galaxy that is just weird I don't know why. That's not a big issue however.

I have a request for you to make, can you make a tutorial showing how to post on to facebook wall and also twitter wall.

I have tried implementing it for facebook but my app crashes.

I have a class that has a button, and I am trying to use that button to call on a facebookactivity class which has the facebook asynctask function for uploads. I also am using a token to save user information so next time they do not have to login.The token is saved in the facebookactivity class I havent been able to see app work because it keeps on crashing.

If you can help with this much thanks!!

Android Er said...

hello chilltime,

Have to read it https://developers.facebook.com/docs/mobile/android/build/.

May be I will try later.

chilltime said...

Thanks for sending me that link it would have taken forever for me to find it. I am trying it now. I will let you know if I am successful. Do you have any example for twitter?

thanks Android-er

patzapps said...

Do you know how to add an overlay wich will also be recorded by the videocam?!
A textview for example which appears in the video (e.g. Time etc.)

Thank you very much :)

Mohsin Ali said...

what to do with this code i cant understand can you explain me plzzzz i want to know how to make video with flashlight my phons software is ics