This section explains how to record a video on Android. For recording video,
The below section throws light on
MediaRecorder
class will be used. To handle the display of camcorder, SurfaceView
and SurfaceHolder.Callback
interface will be used. The below section throws light on
MediaRecorder
, SurfaceView
and SurfaceHolder.Callback
interface. - MediaRecorder: This class is used to record audio and video. It has different methods to configure the recorder. The methods being used in the Video Recorder are explained below:
setVideoSource()
: This sets the video source (i.e. Camera or Default) to be used for recording. If this method is not called, the output file will not contain a video track.setOutputFormat()
: This sets the format of the output file produced during recording. Call this aftersetAudioSource()
/setVideoSource()
but beforeprepare()
.setVideoEncoder
: This sets the video encoder to be used for recording. If this method is not called, the output file will not contain a video track. Call this aftersetOutputFormat()
and beforeprepare()
.setOutputFile()
: This sets the path of the output file. Call this aftersetOutputFormat()
but beforeprepare()
.setPreviewDisplay()
: Sets a Surface to show a preview of recorded media (video). Calls this beforeprepare()
to make sure that the desirable preview display is set.prepare()
: Prepares the recorder to begin capturing and encoding data. This method must be called after setting up the desired audio and video sources, encoders, file format, etc., but beforestart()
.start()
: Begins capturing and encoding data to the file specified withsetOutputFile()
. Call this afterprepare()
.
- SurfaceView: This is a customized view which provides a drawing surface inside the View hierarchy. It is used for resource intensive tasks where rapid updates or high frame rates are required such as 3D graphics, creating games, or previewing the camera in real time. The main advantage of using this view is that it provides an independent thread to update the view, which gives a great user experience.
- SurfaceHolder: As the name suggests, this interface holds the
SurfaceView
object. It allows developers to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface.
VideoRecording
application. We will begin with developing a customized view.- Create the RecorderPreview class which extends the
SurfaceView
andImplements
theSurfaceHolder.Callback
interface.class RecorderPreview extends SurfaceView implements SurfaceHolder.Callback { //Create objects for MediaRecorder and SurfaceHolder. MediaRecorder recorder; SurfaceHolder holder; //Create constructor of Preview Class. In this, get an object of //surfaceHolder class by calling getHolder() method. After that add //callback to the surfaceHolder. The callback will inform when surface is //created/changed/destroyed. Also set surface not to have its own buffers. public Preview(Contect context,MediaRecorder temprecorder) { super(context); recorder=temprecorder; holder=getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } // Implement the methods of SurfaceHolder.Callback interface // SurfaceCreated : This method gets called when surface is created. // In this, initialize all parameters of MediaRecorder object as explained // above. public void surfaceCreated(SurfaceHolder holder){ try{ recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); recorder.setOutputFile("/sdcard/recordvideooutput.3gpp"); recorder.setPreviewDisplay(mHolder.getSurface()); recorder.prepare(); } catch (Exception e) { String message = e.getMessage() } }
(Note: To allow recording video, add permission into the application's manifest file. i.e. Add<uses-permission android:name="android.permission.RECORD_VIDEO"/>
).
// SurfaceDestroyed : This method gets called immediately before the // surface is being destroyed. Stop camera preview because surface will no // longer exist. public void surfaceDestroyed(SurfaceHolder holder) { if(recorder!=null) { recorder.release(); recorder = null; } } //surfaceChanged : This method is called after the surface is created. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { } }
- Implement the onCreate method of video recording application.
//Create objects of MediaRecorder and Preview class private MediaRecorder recorder; private Preview preview; // In this method, create an object of MediaRecorder class. Create an object of // RecorderPreview class(Customized View). Add RecorderPreview class object // as content of UI. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); recorder = new MediaRecorder(); preview = new RecorderPreview(this,recorder); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(preview); } //Create option menu which can be used for starting and stopping video recording. // Also implement the onOptionsItemSelected to handle the events for option menu. public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "StartRecording"); menu.add(0, 1, 0, "StopRecording"); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 0: recorder.start(); break; case 1: recorder.stop(); break; } return super.onOptionsItemSelected(item); }
No comments:
Post a Comment