1 xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<SurfaceView
Android: id = "@ + id/videoView"
Android: layout_width = "300dip"
Android: layout_height = "200dip"
Android: visibility = "visible">
</SurfaceView>
<RelativeLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Button
Android: id = "@ + id/start"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Recording"/>
<Button
Android: id = "@ + id/stop"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_toRightOf = "@ id/start"
Android: text = "stop"/>
</RelativeLayout>
</LinearLayout>
2. java code
Package prox. AndroidRecordVideoTest;
Import java. io. File;
Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import android. app. Activity;
Import android. content. pm. ActivityInfo;
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. widget. Button;
Public class AndroidRecordVideoTest extends Activity {
Private SurfaceView mSurfaceView;
Private SurfaceHolder mSurfaceHolder;
Private Button buttonStart;
Private Button buttonStop;
Private File dir;
Private MediaRecorder recorder;
Private String path = "/sdcard/RTCameraSendRecv /";
SimpleDateFormat format1;
String fileName = "";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Format1 = new SimpleDateFormat ("yyyyMMddHHmmss ");
MSurfaceView = (SurfaceView) findViewById (R. id. videoView );
MSurfaceHolder = mSurfaceView. getHolder ();
MSurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
ButtonStart = (Button) findViewById (R. id. start );
ButtonStop = (Button) findViewById (R. id. stop );
Dir = new File (path );
If (! Dir. exists ()){
Dir. mkdir ();
}
Recorder = new MediaRecorder ();
ButtonStart. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Startrecord ();
}
});
ButtonStop. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Recorder. stop ();
Recorder. reset ();
Recorder. release ();
Recorder = null;
}
});
}
Public void startrecord (){
Try {
FileName = format1.format (new Date ());
Recorder. setPreviewDisplay (mSurfaceHolder. getSurface ());
Recorder. setVideoSource (MediaRecorder. VideoSource. CAMERA );
Recorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP );
Recorder. setVideoSize (352,288 );
Recorder. setVideoFrameRate (25 );
Recorder. setVideoEncoder (MediaRecorder. VideoEncoder. H264 );
Recorder. setMaxDuration (60000); // 60 s
Recorder. setOutputFile (path + fileName + ". 3gp ");
Recorder. prepare ();
Recorder. start ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
3. add permissions to AndroidManifest. xml.
<Uses-permission android: name = "android. permission. CAMERA"/>
<Uses-permission android: name = "android. permission. RECORD_AUDIO"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
In this way, a camera recording program will be completed.