Android Development using Mediarecorder recording video

Source: Internet
Author: User

Mediarecorder can also be used to record video, in addition to being used for recording audio . about Mediarecorder, you can refer to the Android Development of Mediarecorder class explanation ". recording video with Mediarecorder is basically the same as recording audio. Just recording a video requires not only capturing the sound, but also capturing the image. In order for Mediarecorder to capture an image while recording, you should call the Setvideosource (int video source) method when calling the Setaudiosource (int audio source) method to set the image source.

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

1) Call the Mediarecorder object's Setvideoencoder (), Setvideoencodingbitrate (Intbitrate), Setvideoframerate set the encoding format, encoding bitrate, number of frames per second, etc. for recorded video, which will control the quality of the recorded video and the size of the file. In general, 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 : two buttons in 2014.08.14 * <br/> */public class Recordvideo extends activityimplements onclicklistener{//program ImageButton Record, stop;//system of 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 buttons in the program interface record = (ImageButton) Findviewbyid (r.id.record); stop = (ImageButton) Findviewbyid (r.id.stop);// Make the Stop button unavailable. Stop.setenabled (false);//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 the component so that the screen does not automatically turn off Sview.getholder (). Setkeepscreenon (True); @Overridepublic void OnClick (View source) {switch (Source.getid ()) {//Click the Record button Case 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 to collect sound from microphone ( or sound from a VCR audiosource.camcorder) Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);// Set the image collection from the camera Mrecorder.setvideosource (MediaRecorder.VideoSource.CAMERA);//Set the output format of the video file//must be in the format of sound encoding, 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---");//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 you are recording if (isrecording) {//Stop recording mrecorder.stop ();//Release Resources mrecorder.release (); Mrecorder = null;//Make the Record button available. Record.setenabled (TRUE);//Make the Stop button 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 Run preview:



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.