Use MediaRecorder to record videos for Android Development

Source: Internet
Author: User

Use MediaRecorder to record videos for Android Development

In addition to recording audio, MediaRecorder can also be used to record videos. For details about MediaRecorder, refer to MediaRecorder class in Android development. The procedure for recording a video using MediaRecorder is basically the same as that for recording audio. You only need to collect sound and images when recording videos. To enable MediaRecorder to collect images during recording, call the setAudioSource (int audio source) method to set the image source.

In addition, you need to follow the steps below after calling setOutputFormat () to set the output file format:

1) Call setVideoEncoder (), setVideoEncodingBitRate (intbitRate), and setVideoFrameRate of the MediaRecorder object to set the encoding format, encoding bit rate, and frames per second of the recorded video, these parameters can control the quality and file size of the recorded video. Generally, the better the video quality, the larger the video file.

2) Call the setPreviewDisplay (Surfacesv) method of MediaRecorder to set which SurfaceView is used to display the video preview.

The remaining code is basically the same as the recorded audio code:

The instance uses MediaRecorder to record a video:

1. RecordVideo class:

Package com. jph. recordvideo; import java. io. file; import android. app. activity; import android. content. pm. activityInfo; import android. graphics. pixelFormat; import android. media. mediaRecorder; import android. OS. bundle; import android. OS. environment; import android. view. surfaceView; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. view. windowManager; import android. widget. imageButton; import android. widget. toast;/*** Description: * use MediaRecorder to record a video * @ author jph * Date: 2014.08.14 *
*/Public class RecordVideo extends Activityimplements OnClickListener {// two buttons in the program: ImageButton record, stop; // system video File videoFile; MediaRecorder mRecorder; // display SurfaceView sView for video preview; // record whether recording is in progress private boolean isRecording = false; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // remove the title bar, which must be placed before setContentView (Window. FE ATURE_NO_TITLE); setContentView (R. layout. main); // set setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); // set full screen getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); // You can select semi-transparent mode and use it in surfaceview activity. GetWindow (). setFormat (PixelFormat. TRANSLUCENT); // obtain the two buttons record = (ImageButton) findViewById (R. id. record); stop = (ImageButton) findViewById (R. id. stop); // make the stop button unavailable. Stop. setEnabled (false); // bind the listener record to the Click Event of the two buttons. setOnClickListener (this); stop. setOnClickListener (this); // obtain SurfaceViewsView = (SurfaceView) this in the program interface. findViewById (R. id. sView); // sets the resolution sView. getHolder (). setFixedSize (1280,720); // set this component so that sView is not automatically disabled on the screen. getHolder (). setKeepScreenOn (true);} @ Overridepublic void onClick (View source) {switch (source. getId () {// click the recording button case R. id. record: if (! Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED) {Toast. makeText (RecordVideo. this, "the SD card does not exist. Please insert the SD card! ", Toast. LENGTH_SHORT ). show (); return;} try {// create the video File videoFile = new File (Environment. getExternalStorageDirectory (). getCanonicalFile () + "/testvideo.3gp"); // create the MediaPlayer object mRecorder = new MediaRecorder (); mRecorder. reset (); // set the audio from the microphone (or the audio from the video recorder AudioSource. CAMCORDER) mRecorder. setAudioSource (MediaRecorder. audioSource. MIC); // sets the mRecorder for collecting images from the camera. setVideoSource (MediaRecorder. videoSource. CAMERA ); // Set the output format of the video file // you must set the mRecorder before setting the audio encoding format and image encoding format. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP); // sets the audio encoding format mRecorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); // sets the image encoding format mRecorder. setVideoEncoder (MediaRecorder. videoEncoder. h264); mRecorder. setVideoSize (1280,720); // four mRecorder frames per second. setVideoFrameRate (20); mRecorder. setOutputFile (videoFile. getAbsolutePath (); // specifies to use SurfaceView to preview the video mRe Corder. setPreviewDisplay (sView. getHolder (). getSurface (); // ① mRecorder. prepare (); // start recording mRecorder. start (); System. out. println ("--- recording ---"); // make the record button unavailable. Record. setEnabled (false); // make the stop button available. Stop. setEnabled (true); isRecording = true;} catch (Exception e) {e. printStackTrace ();} break; // click the stop button case R. id. stop: // if recording is in progress if (isRecording) {// stop recording mRecorder. stop (); // release the resource mRecorder. release (); mRecorder = null; // make the record button available. Record. setEnabled (true); // make the stop button unavailable. Stop. setEnabled (false) ;}break ;}}}


2. layout file:

 
 
  
  
  
   
   
  
 

3. Add the corresponding permissions to the program:

 
 
 
 
 
 
 

4. Program preview:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.