Simple use of MediaRecorder, mediarecorder
MediaRecorder is used for recording videos. The MediaRecorder recording must be called in sequence according to the API instructions. Otherwise, an error is returned. You may not be able to call the start () method or call start () to crash. MediaRecorder depends on hardware and can be used on a mobile phone. If it is placed on another mobile phone, a bug may occur.
Add permission:
<User-permission android: name = "android. permission. RECORD_AUdio"/>
<User-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<User-permission android: name = "android. permission. CAMERA"/>
<User-permission android: name = "android. permission. FLASHLIGHT"/>
<User-permission android: name = "android. permission. MOUNT_UNMOUNT_FILRSYSTEMS"/>
<User-permission android: name = "android. hardware. camera"/>
<User-permission android: name = "android. hardware. camera. autofocus"/>
The usage of MediaRecorder is as follows:
// Set the called Camera
Mediarecorder. setCamera (Camera );
// Specify the Audio and video sources
Mediarecorder. setAudioSource (MediaRecorder. AudioSource. CAMCORDER );
Mediarecorder. setVideoSource (MediaRecorder. VideoSource. CAMERA );
// Specify the CamcorderProfile (API Level 8 or above is required)
// MMediaRecorder. setProfile (CamcorderProfile. get (CamcorderProfile. QUALITY_HIGH ));
//If you use CamcorderProfile for configuration, do not write the output format, audio encoding, and video encoding.
// Set the output format and encoding format (for versions earlier than API Level 8)
Mediarecorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP); // set the output format. THREE_GPP is 3gp, And. MPEG_4 is mp4.
Mediarecorder. setAudioEncoder (MediaRecorder. AudioEncoder. DEFAULT); // sets the Audio Encoding type mic.
Mediarecorder. setVideoEncoder (MediaRecorder. VideoEncoder. H264); // sets the video encoding type. Generally, h263, h264
Mediarecorder. setOutputFile ("/sdcard/myVideo.3gp ");
Mediarecorder. setVideoSize (640,480); // sets the video resolution,When start () is called incorrectly, an error is reported,Comments can be dropped to run the program test, and sometimes comments can be run.
// Mediarecorder. setVideoFrameRate (24); // sets the video frame rate, which can be omitted
Mediarecorder. setVideoEncodingBitRate (10*1024*1024); // raise the frame frequency, blur the video, flat the screen, and write the green screen for debugging.
Mediarecorder. setPreviewDisplay (surfaceHolder. getSurface (); // sets video preview.
Try {
// Prepare for recording
Mediarecorder. prepare ();
// Start recording
Mediarecorder. start ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Stop video recording: (call the following method in sequence)
Surfaceview = null;
SurfaceHolder = null;
If (mediarecorder! = Null ){
// Stop recording
Mediarecorder. stop (); // stop first
Mediarecorder. reset (); // reset mediarecorder
// Release resources
Mediarecorder. release (); // release mediarecorder
Mediarecorder = null;
If (mCamera! = Null ){
MCamera. release (); // release the camera
MCamera = null;
}
}
Video encoding format: default, H263, H264, MPEG_4_SP
Obtain video resources: default, CAMERA
Audio Encoding format: default, AAC, AMR_NB, AMR_WB,
Obtain audio resources: defalut, camcorder, mic, voice_call, voice_communication, voice_downlink, voice_recognition, voice_uplink;
Output Method: amr_nb, amr_wb, default, mpeg_4, raw_amr, three_gpp.