Here is a demo of your own test, inside the note is very detailed. 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 recorded video example <BR> * PS: Achieve basic Recording save File <BR> * * @ve Rsion 1.00 2011/09/21 * @author codyy) peijiangping*/ Public classTestbasicvideo extends Activity implements Surfaceholder.callback {PrivateButton start;//Start Recording button PrivateButton stop;//Stop Recording button PrivateMediarecorder Mediarecorder;//class for recording video PrivateSurfaceview Surfaceview;//controls that display video//used to display a video interface, I do not have to not yet, that is, to record video with Mediarecorder and to give an interface to see//students who want to secretly record video can consider other ways. Well, you need to implement the callback interface for this interface. PrivateSurfaceholder Surfaceholder; Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (window.feature_no_title);//Remove title barGetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FL Ag_fullscreen);//Set Full Screen//Setting the horizontal screen displaysetrequestedorientation (Activityinfo.screen_orientation_landscape); //choose to support semitransparent mode, which is used in surfaceview activity. GetWindow (). SetFormat (pixelformat.translucent); Setcontentview (R.layout.main); Init (); } Private voidInit () {Start= (Button) This. Findviewbyid (R.id.start); Stop= (Button) This. Findviewbyid (R.id.stop); Start.setonclicklistener (NewTestvideolistener ()); Stop.setonclicklistener (NewTestvideolistener ()); Surfaceview= (Surfaceview) This. Findviewbyid (R.id.surfaceview); Surfaceholder Holder= Surfaceview.getholder ();//Get HolderHolder.addcallback ( This);//Holder Join Callback interface//Settype must be set, or error-prone.Holder.settype (surfaceholder.surface_type_push_buffers); } classTestvideolistener implements Onclicklistener {@Override Public voidOnClick (View v) {if(v = =start) {Mediarecorder=NewMediarecorder ();//Create a Mediarecorder object//set the recording video source to cameraMediarecorder.setvideosource (MediaRecorder.VideoSource.CAMERA); //set the format of the video after recording is complete THREE_GPP to 3gp.mpeg_4 for MP4mediarecorder. Setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); //set the recorded video encoding h263 H264Mediarecorder.setvideoencoder (MediaRecorder.VideoEncoder.H264); //sets the resolution of the video recording. Must be placed behind the set encoding and format, otherwise the errorMediarecorder.setvideosize (176,144); //sets the recorded video frame rate. Must be placed behind the set encoding and format, otherwise the errorMediarecorder.setvideoframerate ( -); Mediarecorder.setpreviewdisplay (Surfaceholder.getsurface ()); //set the path of the video file outputMediarecorder.setoutputfile ("/sdcard/love.3gp"); Try { //Prepare to recordMediarecorder.prepare (); //Start RecordingMediarecorder.start (); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } if(v = =stop) { if(Mediarecorder! =NULL) { //Stop RecordingMediarecorder.stop (); //Freeing Resourcesmediarecorder.release (); Mediarecorder=NULL; } }} @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) { //will holder, this holder to start in oncreat inside the holder, to assign it to SurfaceholderSurfaceholder =Holder; } @Override Public voidsurfacecreated (Surfaceholder holder) {//will holder, this holder to start in oncreat inside the holder, to assign it to SurfaceholderSurfaceholder =Holder; } @Override Public voidsurfacedestroyed (Surfaceholder holder) {//surfacedestroyed at the same time object is 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:minsdkver sion="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>
Use Mediarecorder for video recording in Android