Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create the <strong>RecorderPreview</strong> class which extends the <em>SurfaceView</em> and <strong>Implements</strong> the <em>SurfaceHolder.Callback</em> interface.</p> <pre><code>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() } } </code></pre> <p>To allow recording video, add permission into the application's manifest file. </p> <pre><code> &lt;uses-permission android:name="android.permission.RECORD_VIDEO"/&gt; 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) { } } </code></pre> <p>Implement the onCreate method of video recording application.</p> <pre><code>//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); } </code></pre> <p>You can find original code here: </p> <p><a href="http://www.codeproject.com/Articles/107270/Recording-and-Playing-Video-on-Android" rel="nofollow">http://www.codeproject.com/Articles/107270/Recording-and-Playing-Video-on-Android</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload