Mediarecorder in addition to using recorded audio . can also be used to record video. we can refer to the specific explanation of Mediarecorder Android Development of Mediarecorder Class Specific explanations ". the steps to record video and record audio using Mediarecorder are basically the same. Just recording a video requires not only a collection of sounds. You also need to set the image. In order for Mediarecorder to capture the image when it is recorded, you should call the Setvideosource (int video source) method when calling the Setaudiosource (int audio source) method to set the image source.
In addition to this, you need to set the output file format after calling Setoutputformat () such as the following steps:
1) Call the Mediarecorder object's Setvideoencoder (), Setvideoencodingbitrate (Intbitrate), Setvideoframerate sets the encoding format, encoding bitrate, how many frames per second, and so on for the recorded video. These parameters will be able to control the quality of the recorded video and the size of the file.
Generally speaking. The better the video quality, the larger the video file.
2) call Mediarecorder's Setpreviewdisplay (SURFACESV) method to set which Surfaceview to use to display the video preview.
The rest of the code is basically the same as the code for recording audio :
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: * Record video with Mediarecorder * @author JPH * Date : 2014.08.14 * <br/> */public class Recordvideo extends Activityimplements onclicklistener{// Program of two Buttonimagebutton record, stop;//system video file videofile; Mediarecorder mrecorder;//Display video preview of Surfaceview Surfaceview Sview; Record if recording is in progress private Boolean isrecording = false; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Remove the title bar, Must be placed before Setcontentview requestwindowfeature (window.feature_no_title); Setcontentview (R.LAyout.main); Set the horizontal screen display setrequestedorientation (activityinfo.screen_orientation_landscape); Set full screen GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, Windowmanager.layoutparam S.flag_fullscreen); Choose to support semitransparent mode, which is used in surfaceview activity. GetWindow (). SetFormat (pixelformat.translucent); Get two Buttonrecord = (ImageButton) Findviewbyid (R.id.record) in the program interface; stop = (ImageButton) Findviewbyid (r.id.stop);// Make Stopbutton unavailable. Stop.setenabled (false);//A Click event binding listener for two buttons Record.setonclicklistener (this); Stop.setonclicklistener (this);// Gets the Surfaceviewsview = (Surfaceview) This.findviewbyid (R.id.sview) in the program interface; Set resolution Sview.getholder (). Setfixedsize (1280, 720); Set this component so that the screen does not voluntarily turn off Sview.getholder (). Setkeepscreenon (True); @Overridepublic void OnClick (View source) {switch (Source.getid ()) {//click Record Buttoncase r.id.record:if (! Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) {TOAST.MAketext (Recordvideo.this, "SD card does not exist, please insert SD card! ", Toast.length_short). Show (); return;} try{//Create a video file that saves the recorded video videofile = new file (Environment.getexternalstoragedirectory (). Getcanonicalfile () + "/ TESTVIDEO.3GP ");//Create MediaPlayer Object mrecorder = new Mediarecorder (); Mrecorder.reset ();//Set the sound from the microphone set ( or sound from a VCR audiosource.camcorder) Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);// Set the image from the camera Mrecorder.setvideosource (MediaRecorder.VideoSource.CAMERA);//Set the output format of the video file//must be set in the sound encoding format, Image encoding format before setting Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Set the format of the sound encoding Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);//Set the format of the image encoding Mrecorder.setvideoencoder (MediaRecorder.VideoEncoder.H264); Mrecorder.setvideosize (1280, 720);//4 frames per second mrecorder.setvideoframerate (mrecorder.setoutputfile); Videofile.getabsolutepath ());//Specifies to use Surfaceview to preview the video Mrecorder.setpreviewdisplay (Sview.getholder (). GetSurface ()) ; ①mrecorder.prepare ();//Start recording mrecorder.start (); SYSTEM.OUT.PRINTLN ("---recording---");//Let RecordbutTon is not available. Record.setenabled (false);//Make Stopbutton available.Stop.setenabled (true); isrecording = true;} catch (Exception e) {e.printstacktrace ();} break;//Click Stop Buttoncase r.id.stop://assume the recording is in progress if (isrecording) {//Stop recording mrecorder.stop ();//Release Resources mrecorder.release (); Mrecorder = null;//makes Recordbutton available. Record.setenabled (TRUE);//Make Stopbutton unavailable. Stop.setenabled (false);} Break;}}}
2. layout file:
<?xml version= "1.0" encoding= "Utf-8"?><relativelayoutxmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Match_parent" android:layout_height= "Match_parent" ><!--Show video preview Surfaceview-->< Surfaceviewandroid:id= "@+id/sview" android:layout_width= "match_parent" android:layout_height= "Match_parent"/> <linearlayoutandroid:orientation= "Horizontal" android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:gravity=" Center_horizontal "android:layout_alignparentbottom=" true "Android:layout_ Centerhorizontal= "true" ><imagebuttonandroid:id= "@+id/record" android:layout_width= "66DP" Android:layout_ height= "66DP" android:scaletype= "Fitcenter" android:src= "@drawable/recored"/><imagebuttonandroid:id= "@+id/ Stop "android:layout_width=" 66DP "android:layout_height=" 66DP "android:scaletype=" Fitcenter "android:src=" @ Drawable/stop "/></linearlayout></relativelayout>
3. Add the appropriate permissions for the program:
<!--grant the program the right to record sound--><uses-permission android:name= "Android.permission.RECORD_AUDIO"/><!-- Grant the program permission to use the webcam--><uses-permission android:name= "Android.permission.CAMERA"/><uses-permission android: Name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!--grant permission to use external storage--><uses-permission Android: Name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
4. Program Execution preview:
Android uses development Mediarecorder to record video