Android camera recording

Source: Internet
Author: User

1 xml


<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<SurfaceView
Android: id = "@ + id/videoView"
Android: layout_width = "300dip"
Android: layout_height = "200dip"
Android: visibility = "visible">
</SurfaceView>

<RelativeLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">

<Button
Android: id = "@ + id/start"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Recording"/>

<Button
Android: id = "@ + id/stop"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_toRightOf = "@ id/start"
Android: text = "stop"/>
</RelativeLayout>

</LinearLayout>

 

 

 

2. java code

Package prox. AndroidRecordVideoTest;

Import java. io. File;
Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. Date;

Import android. app. Activity;
Import android. content. pm. ActivityInfo;
Import android. media. MediaRecorder;
Import android. OS. Bundle;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class AndroidRecordVideoTest extends Activity {
Private SurfaceView mSurfaceView;
Private SurfaceHolder mSurfaceHolder;
Private Button buttonStart;
Private Button buttonStop;
Private File dir;
Private MediaRecorder recorder;
Private String path = "/sdcard/RTCameraSendRecv /";
SimpleDateFormat format1;
String fileName = "";

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Format1 = new SimpleDateFormat ("yyyyMMddHHmmss ");
MSurfaceView = (SurfaceView) findViewById (R. id. videoView );

MSurfaceHolder = mSurfaceView. getHolder ();
MSurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
ButtonStart = (Button) findViewById (R. id. start );
ButtonStop = (Button) findViewById (R. id. stop );


Dir = new File (path );
If (! Dir. exists ()){
Dir. mkdir ();
}
Recorder = new MediaRecorder ();

ButtonStart. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Startrecord ();
}
});

ButtonStop. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Recorder. stop ();
Recorder. reset ();
Recorder. release ();
Recorder = null;
}
});
}

Public void startrecord (){
Try {
FileName = format1.format (new Date ());

Recorder. setPreviewDisplay (mSurfaceHolder. getSurface ());

Recorder. setVideoSource (MediaRecorder. VideoSource. CAMERA );

Recorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP );

Recorder. setVideoSize (352,288 );

Recorder. setVideoFrameRate (25 );

Recorder. setVideoEncoder (MediaRecorder. VideoEncoder. H264 );

Recorder. setMaxDuration (60000); // 60 s

Recorder. setOutputFile (path + fileName + ". 3gp ");
Recorder. prepare ();
Recorder. start ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

 

 

 

3. add permissions to AndroidManifest. xml.

<Uses-permission android: name = "android. permission. CAMERA"/>
<Uses-permission android: name = "android. permission. RECORD_AUDIO"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

 

 

 

 


In this way, a camera recording program will be completed.


 

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.