Use Mediarecorder for video recording in Android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.