MainActivity is as follows:
Package c. c; import java. io. file; import java. io. IOException; import android. app. activity; import android. content. pm. activityInfo; import android. hardware. camera; 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 androi D. view. view. onClickListener; import android. view. window; import android. view. windowManager; import android. widget. button;/*** Demo Description: * used SurfaceView to take a video. The preview function is added to the previous version. ** Note: * 1. Important Note: Set the MediaRecorder parameter. 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); * Correction: 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. ** Note: * from Android recording video (v) to Android recording video (I) Difficulty * gradually increases and gradually improves. **/public class MainActivity extends Activity implements SurfaceHolder. callback {private Button mStartButton; private Button mStopButton; private Camera mCamera; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private MediaRecord Er mMediaRecorder; @ Override public void onCreate (Bundle savedInstanceState) {super. 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); mSurfa CeHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS);} private void initMediaRecorder () {// next codes is right for 2.3 and 4.0 mMediaRecorder = new MediaRecorder (); mMediaRecorder. setCamera (mCamera); // 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 (MediaR Ecorder. outputFormat. DEFAULT); // sets the video encoding method mMediaRecorder. setVideoEncoder (MediaRecorder. videoEncoder. DEFAULT); // sets the audio encoding method 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 () B Ut before prepare (). mMediaRecorder. setVideoFrameRate (15); // sets the mMediaRecorder of the preview image. setPreviewDisplay (mSurfaceHolder. 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. setOnInfoL Istener (new OnInfoListener () {public void onInfo (MediaRecorder mr, int what, int extra) {if (what = MediaRecorder. 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) {// mCamera. lock (); mCamera. stopPreview (); mMediaRecorder. stop (); mMediaRecorder. release (); mMediaRecorder = null ;}}// SurfaceHolder. callback interface public void surfaceCreated (SurfaceHolder holder) {System. out. println ("SurfaceView ----> Created"); try {mCamera = Camera. open (); mCamera. setPreviewDisplay (mSurfaceHolder); mCamera. startPreview (); mCamera. unlock ();} catch (IOException e) {// TODO Auto-generated Catch blocke. printStackTrace () ;}} 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 is as follows:
<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 Android: Id = "@ + ID/stop_button" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: TEXT = "stop" Android: layout_weight = "1"/> </linearlayout>