Using mediarecorder in Android for video recording (video recording)

Source: Internet
Author: User

Here is a test demo of your own, which is described in detail. Simple video recording function.

Package COM. video; import Java. io. ioexception; import android. app. activity; import android. content. PM. activityinfo; import android. graphics. pixelformat; import android. media. mediarecorder; import android. OS. bundle; import android. view. surfaceholder; import android. view. surfaceview; import android. view. view; import android. view. view. onclicklistener; import android. view. window; import android. view. windowmanager; Import android. widget. button;/*** Class Name: testbasicvideo <br> * class description: A simple recording video example <br> * PS: implement basic recording and saving files <br> ** @ Version 1.00 2011/09/21 * @ author codyy) peijiangping */public class testbasicvideo extends activity implements surfaceholder. callback {private button start; // The start recording button private button stop; // The stop recording button private mediarecorder; // The class private surfaceview of the recorded video ;/ /Display the video control // an interface used to display the video. I don't need it anymore, that is to say, if you want to use mediarecorder to record a video, you have to watch the video on the page .. Well, you need to implement the callback interface private surfaceholder for this interface; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); // remove the title bar getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); // sets full screen // sets setrequestedorientation (activityinfo. screen_orientation_landscape); // You can select to support the semi-transparent mode. Urfaceview activity. Getwindow (). setformat (pixelformat. translucent); setcontentview (R. layout. main); Init ();} private void Init () {start = (button) This. findviewbyid (R. id. start); stop = (button) This. findviewbyid (R. id. stop); start. setonclicklistener (New testvideolistener (); stop. setonclicklistener (New testvideolistener (); surfaceview = (surfaceview) This. findviewbyid (R. id. surfaceview); surfaceholder holder = surfaceview. getho Lder (); // get holderholder. addcallback (this); // The callback interface added by holder // settype must be set to avoid errors. holder. settype (surfaceholder. surface_type_push_buffers);} class testvideolistener implements onclicklistener {@ overridepublic void onclick (view v) {If (V = Start) {mediarecorder = new mediarecorder (); // create a mediarecorder object // set the recorded video source to camera (CAMERA) mediarecorder. setvideosource (mediarecorder. videosource. camera); // sets the video Encapsulation Format after recording. Three_gpp is 3GP. mpeg_4 is mp4mediarecorder. setoutputformat (mediarecorder. outputformat. three_gpp); // sets the recorded video encoding h263 hsf-mediarecorder. setvideoencoder (mediarecorder. videoencoder. h264); // sets the video recording resolution. It must be placed behind the encoding and format. Otherwise, the error mediarecorder. setvideosize (176,144) is returned. // you can specify the recorded video frame rate. It must be placed after the encoding and format. Otherwise, the error mediarecorder is reported. setvideoframerate (20); mediarecorder. setpreviewdisplay (surfaceholder. getsurface (); // sets the video file output path mediarecorder. setoutputfile ("/sdcard/love.3gp"); try {// prepare the recording mediarecorder. prepare (); // start recording mediarecorder. start ();} catch (illegalstateexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstac Ktrace () ;}} if (V = stop) {If (mediarecorder! = NULL) {// stop recording mediarecorder. stop (); // release the resource mediarecorder. release (); mediarecorder = NULL ;}}}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {// set holder, this holder is the holder that was obtained in oncreat and assigned it to surfaceholdersurfaceholder = holder;} @ overridepublic void surfacecreated (surfaceholder holder) {// change holder, this holder is the holder that was obtained in oncreat and assigned it to surfaceholdersurfaceholder = holder;} @ overridepublic void surfacedestroyed (surfaceholder holder) {// when surfacedestroyed is used, the object is also set to nullsurfaceview = NULL; surfaceholder = NULL; mediarecorder = NULL ;}}

Main. xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1" >        <SurfaceView            android:id="@+id/surfaceview"            android:layout_width="fill_parent"            android:layout_height="fill_parent" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="4"        android:gravity="center"        android:orientation="vertical" >        <Button            android:id="@+id/start"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Start" />        <Button            android:id="@+id/stop"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Stop" />    </LinearLayout></LinearLayout>

Androidmanifest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.video"    android:versionCode="1"    android:versionName="1.0" >    <application        android:icon="@drawable/icon"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".TestBasicVideo" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <uses-sdk android:minSdkVersion="8" />    <uses-permission android:name="android.permission.CAMERA" >    </uses-permission>    <uses-permission android:name="android.permission.RECORD_AUDIO" >    </uses-permission>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >    </uses-permission></manifest>

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.