MainActivit: [java] package c. c; import java. io. file; import java. io. IOException; import android. app. activity; import android. content. pm. activityInfo; import android. media. mediaRecorder; import android. media. mediaRecorder. onInfoListener; import android. OS. bundle; import android. OS. environment; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. view; import and Roid. view. view. onClickListener; import android. view. window; import android. view. windowManager; import android. widget. button;/*** Demo Description: * use SurfaceView to take a video ** Note: * 1 critical note: MediaRecorder parameter settings. differences between mobile phones * 2 when setting MediaRecorder parameters, you should first set: * setVideoSource (), setAudioSource (), setOutputFormat (), setVideoEncoder (), setAudioEncoder * and then set the remaining parameters to check whether the corresponding api of the method prompts the * 3 error: mMediaRecorder. setVideoFrameRate (20); * correct: MMediaRecorder. setVideoFrameRate (15); * symptom: normal on 2.3, error on 4.0 * cause: 12-15 frames per second are enough to indicate motion, and the TV is 30. **/public class MainActivity extends Activity implements SurfaceHolder. callback {private Button mStartButton; private Button mStopButton; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private MediaRecorder mMediaRecorder; @ Override public void onCreate (Bundle savedInstanceState) {su Per. onCreate (savedInstanceState); // remove the title bar requestWindowFeature (Window. FEATURE_NO_TITLE); // sets full screen getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); // sets setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); setContentView (R. layout. main); init ();} private void init () {mStartButton = (Button) findViewById (R. id. start_button ); MStartButton. setOnClickListener (new ButtonClickListenerImpl (); mStopButton = (Button) findViewById (R. id. stop_button); mStopButton. setOnClickListener (new ButtonClickListenerImpl (); mSurfaceView = (SurfaceView) findViewById (R. id. surfaceView); mSurfaceHolder = mSurfaceView. getHolder (); mSurfaceHolder. addCallback (this); mSurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS);} private void ini TMediaRecorder () {// next codes is right for 2.3 and 4.0 mMediaRecorder = new MediaRecorder (); // sets the video source mMediaRecorder. setVideoSource (MediaRecorder. videoSource. DEFAULT); // set the audio source mMediaRecorder. setAudioSource (MediaRecorder. audioSource. DEFAULT); // set the file output format to mMediaRecorder. setOutputFormat (MediaRecorder. outputFormat. DEFAULT); // sets the video encoding method mMediaRecorder. setVideoEncoder (MediaRecorder. videoEncoder. DEFAULT );// Set the audio encoding method to mMediaRecorder. setAudioEncoder (MediaRecorder. audioEncoder. DEFAULT); // set the video height and width. Note the instructions in this document: // Must be called after setVideoSource (). // Call this after setOutFormat () but before prepare (). // set the Frame Rate of the recorded video. Note the instructions in this document: // Must be called after setVideoSource (). // Call this after setOutFormat () but before prepare (). mMediaRecorder. setVideoFrameRate (15); // sets the mMediaRecorder of the preview image. setPreviewDisplay (mSurfaceHol Der. getSurface (); // set the output path to mMediaRecorder. setOutputFile (Environment. getExternalStorageDirectory () + File. separator + System. currentTimeMillis () + ". mp4 "); mMediaRecorder. setVideoSize (800,480); // sets the maximum video duration (mMediaRecorder. setMaxDuration (10000); // set the listener mMediaRecorder for MediaRecorder. setOnInfoListener (new OnInfoListener () {public void onInfo (MediaRecorder mr, int what, int extra) {if (what = MediaRecord Er. MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {System. out. println ("the maximum recording time has been reached"); if (mMediaRecorder! = Null) {mMediaRecorder. stop (); mMediaRecorder. release (); mMediaRecorder = null ;}}});} private class ButtonClickListenerImpl implements OnClickListener {public void onClick (View v) {if (v. getId () = R. id. start_button) {try {initMediaRecorder (); mMediaRecorder. prepare (); mMediaRecorder. start ();} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} If (v. getId () = R. id. stop_button) {if (mMediaRecorder! = Null) {mMediaRecorder. stop (); mMediaRecorder. release (); mMediaRecorder = null ;}}// SurfaceHolder. callback interface public void surfaceCreated (SurfaceHolder holder) {System. out. println ("SurfaceView ----> Created");} public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {System. out. println ("SurfaceView ----> Changed");} public void surfaceDestroyed (SurfaceHolder holder ){ System. out. println ("SurfaceView ----> Destroyed"); if (mMediaRecorder! = Null) {mMediaRecorder. stop (); mMediaRecorder. release (); mMediaRecorder = null ;}} main. xml as follows [html] <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <SurfaceView android: id = "@ + id/surfaceView" android: layout_width = "fill_parent" android: layout_height = "0dip" android: layout_weight = "1"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/start_button" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "" android: layout_weight = "1"/> <Button www.2cto.com android: id = "@ + id/stop_button" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "stop" android: layout_weight = "1"/> </LinearLayout>