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. wi Ndowmanager; 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 surfac of the recorded video Eview surfaceview; // video display control // an interface used to display videos. 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); // select supported Translucent mode, which is used in a surfaceview 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 = surfac Eview. getholder (); // get Holder. 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 {@ override public 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. cam ERA); // set the video Encapsulation Format three_gpp to 3GP after recording. mpeg_4 is MP4 mediarecorder. setoutputformat (mediarecorder. outputformat. three_gpp); // sets the recorded video encoding h263 h264 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 Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch BL Ock E. printstacktrace () ;}} if (V = stop) {If (mediarecorder! = NULL) {// stop recording mediarecorder. stop (); // release the resource mediarecorder. release (); mediarecorder = NULL ;}}}@ override public void surfacechanged (surfaceholder holder, int format, int width, int height) {// set holder, this holder is the holder that is obtained in oncreat. It is assigned to surfaceholder = holder;} @ override public void surfacecreated (surfaceholder holder) {// change holder, this holder is the holder that was obtained in oncreat and assigned it to surfaceholder = holder;} @ override public void surfacedestroyed (surfaceholder holder) {// when surfacedestroyed is set, the object is also set to null surfaceview = 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>