set up a simple interface for video recording
<surfaceview
Android:id= "@+id/surface"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
<linearlayout
Android:layout_above= "@+id/surface"
Android:layout_width= "Match_parent"
Android:layout_alignparentbottom= "true"
Android:layout_centerinparent= "true"
android:orientation= "Horizontal"
android:layout_height= "Wrap_content" >
<button
Android:id= "@+id/start"
Android:text= "Recording"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<button
android:layout_below= "@+id/start"
Android:id= "@+id/stop"
android:text= "Stop"
Android:enabled= "false"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
Java code
Part I: Declaration
Private Button start,stop;
Private Surfaceview surface;
Private Mediarecorder recorder;
Private Boolean isrecording;//state of recording
Part II: Loading Data
private void Initview () {
Start= (Button) Findviewbyid (R.id.start);
stop= (Button) Findviewbyid (r.id.stop);
Stop.setonclicklistener (this);
Start.setonclicklistener (this);
Surface= (Surfaceview) Findviewbyid (r.id.surface);
Setting the horizontal screen display
Setrequestedorientation (Activityinfo.screen_orientation_landscape);
Set up full screen display
GetWindow (). SetFlags (Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);
Set Translucent
GetWindow (). SetFormat (pixelformat.translucent);
Setting the Surfaceview, resolution settings
Surface.getholder (). Setfixedsize (1280,720);
Set the screen to solid
Surface.getholder (). Setkeepscreenon (True)
Recorder=new Mediarecorder ();
}
Part III: Start recording
Start recording
public void startrecording () {
Initialization
Recorder.setaudiosource (MediaRecorder.AudioSource.MIC);
Recorder.setvideosource (MediaRecorder.VideoSource.CAMERA);
Set the recording parameters of the video, format the video
Recorder.setoutputformat (MediaRecorder.OutputFormat.MPEG_4);
Set the encoding of the audio
Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
Set the encoding format for your video
Recorder.setvideoencoder (MediaRecorder.VideoEncoder.H264);
Set the input video size
Recorder.setvideosize (176, 144);
Set a video frame number
Recorder.setvideoframerate (20);
Set the path for video storage
try {
File file =new file (Environment.getexternalstoragedirectory (). Getcanonicalpath (), "Myvideo.mp4");
Recorder.setoutputfile (File.getabsolutepath ());
Set preview
Recorder.setpreviewdisplay (Surface.getholder (). Getsurface ());
Prepare to record
Recorder.prepare ();
} catch (IOException e) {
E.printstacktrace ();
}
Recorder.start ();
Change Recording status
Isrecording=true;
}
The rest of the code refer to the previous article;
Use Mediarecorder for video recording in Android