Saturday, November 10, 2012

Record Video And PlayBack In android

Let us know how record video with audio in andriod .

Firstly device must have in-built camera with device for that use a uses-feature tag in AndroidManifest file
<uses-feature android:name="android.hardware.camera" />

and some permission are required to use camera or write a file in External storage
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

android.view.SurfaceView hold the android camera object and android.media.MediaRecorder object records the video and audio and export the file in java.io.File In Android having two camera rear and front camera, which one can select, for that we use front camera ... by following code
CameraInfo cameraInfo = new CameraInfo(); 
int cameraCount = Camera.getNumberOfCameras();
if(cameraCount >1)
for ( int camIdx = 0; camIdx < Camera.getNumberOfCameras(); camIdx++ ) { 
       Camera.getCameraInfo( camIdx, cameraInfo ); 
       if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) { 
           try { 
            prCamera = Camera.open( camIdx ); 
           } catch (RuntimeException e) { 
               Log.i("Camera failed to open: ",e.getLocalizedMessage()); 
           } 
       } 
   }
In this example video encoded format is MPEG_4_SP, audio format is AMR_NB, and period of recording is 2 minutes and output file name create it by this function
String.valueOf(System.currentTimeMillis()) +".mp4"
after recording PlayVideo.java is used for play video file inside the android.widget.VideoView

Whole source of this post avaiable at my github Repository VideoRecording

No comments:

Post a Comment