Use mediarecoder to record videos

Source: Internet
Author: User

In addition to recording audio, mediarecoder can also be used to record a video. The procedure is basically the same as that of the recorded audio. However, to record a video, you must not only collect the sound but also the image, you need to use surfaceview to preview and display the image. The following is a simple example. The Code is as follows:

Activity:

Package COM. home. recordvideo; import Java. io. file; import android. app. activity; import android. media. mediarecorder; import android. OS. bundle; import android. OS. environment; import android. view. surfaceholder; import android. view. surfaceview; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class recordvedioactivity extends acti Incluimplements onclicklistener {private button recordbtn, stopbtn; // Private file videofile; private mediarecorder mrecorder; // surfaceviewprivate surfaceview sview with video preview displayed; // record whether recording is in progress Private Boolean isrecording; private surfaceholder sholder; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); recordbtn = (button) Fin Dviewbyid (R. id. main_btn_record); stopbtn = (button) findviewbyid (R. id. main_btn_stop); // make the stopbtn button unavailable. setenabled (false); stopbtn. setonclicklistener (this); recordbtn. setonclicklistener (this); // obtain surfaceviewsview = (surfaceview) findviewbyid (R. id. main_sview); sholder = sview. getholder (); // you do not need to maintain the sholder for surfaceview settings. settype (surfaceholder. surface_type_push_buffers); // sets the sholder resolution. setfixe Dsize (800,480); // set this component so that the sholder will not be automatically disabled on the screen. setkeepscreenon (true) ;}@ overridepublic void onclick (view v) {If (V = recordbtn) {If (! Environment. media_mounted.equals (environment. getexternalstoragestate () {toast. maketext (recordvedioactivity. 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 () + "/yvideo.mp4"); // create the mediarecorder object mrecorder = new mediarecorder (); mrecorder. reset (); // set the mrecorder for collecting sound from the microphone. 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 (which must be set before the audio encoding format and image encoding format. setoutputformat (mediarecorder. outputformat. mpeg_4); // sets the audio encoding format mrecorder. setaudioencoder (mediarecorder. audioencoder. default); // sets the image encoding format mrecorder. setvideoencoder (mediarecorder. videoencoder. mpeg_4_sp); mrecorder. setvideosize (800,480); // four mrecorder frames per second. setvideoframerate (4); mrecorder. setoutputfile (videofile. getabsolutepath (); // specifies to use surfaceview to preview the video mrecorder. setpreviewdisplay (sview. getholder (). getsurface (); mrecorder. prepare (); // make record button unavailable recordbtn. setenabled (false); // set the stopbtn button to stopbtn. setenabled (true); isrecording = true; // start recording mrecorder. start (); system. out. println ("----- recording -----");} catch (exception e) {e. printstacktrace () ;}}if (V = stopbtn) {// if the video is being recorded if (isrecording) {// stop recording mrecorder. stop (); // release the resource mrecorder. release (); mrecorder = NULL; // make record buttons available in recordbtn. setenabled (true); // disable the stopbtn button. setenabled (false );}}}}

Layout XML:

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: gravity = "center_horizontal" Android: Orientation = "horizontal"> <button Android: id = "@ + ID/main_btn_record" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "recording video"/> <button Android: id = "@ + ID/main_btn_stop" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "stop recording"/> </linearlayout> <surfaceview Android: Id = "@ + ID/main_sview" Android: layout_width = "match_parent" Android: layout_height = "match_parent"/> </linearlayout>

Add the following permissions:

<! -- Authorize this program to record sound --> <uses-Permission Android: Name = "android. Permission. record_audio"/> <! -- Grant the App the permission to use the camera --> <uses-Permission Android: Name = "android. permission. camera "/> <uses-Permission Android: Name =" android. permission. mount_unmount_filesystems "/> <! -- Grant the program permission to use external storage --> <uses-Permission Android: Name = "android. Permission. write_external_storage"/>

 

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.