Using MediaRecorder in Android for video recording (video recording)

Source: Internet
Author: User

 

Here is a test DEMO of your own, which is described in detail. Simple video recording function.

 

 

Package com. video;

 

Import java. io. IOException;

 

Import android. app. Activity;

Import android. content. pm. ActivityInfo;

Import android. graphics. PixelFormat;

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. view. Window;

Import android. view. WindowManager;

Import android. widget. Button;

 

/**

* Class name: TestBasicVideo <BR>

* Class description: A simple recording video example <BR>

* PS: basic recording and saving files <BR>

*

* @ Version 1.00

* @ Author CODYY) peijiangping

*/

Public class TestBasicVideo extends Activity implements SurfaceHolder. Callback {

Private Button start; // the start Button.

Private Button stop; // the stop Button.

Private MediaRecorder mediarecorder; // specifies the video recording class.

Private SurfaceView surfaceview; // controls for displaying videos

// An interface used to display videos. I don't need it anymore. That is to say, I have to display the video recorded using mediarecorder.

// Other methods can be considered for video recording .. Well, you need to implement the Callback interface of this interface.

Private SurfaceHolder surfaceHolder;

 

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

RequestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title bar.

GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,

WindowManager. LayoutParams. FLAG_FULLSCREEN); // set full screen

// Set Landscape Display

SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );

// Select semi-transparent mode and use it in surfaceview activity.

GetWindow (). setFormat (PixelFormat. TRANSLUCENT );

SetContentView (R. layout. main );

Init ();

}

 

Private void init (){

Start = (Button) this. findViewById (R. id. start );

Stop = (Button) this. findViewById (R. id. stop );

Start. setOnClickListener (new TestVideoListener ());

Stop. setOnClickListener (new TestVideoListener ());

Surfaceview = (SurfaceView) this. findViewById (R. id. surfaceview );

SurfaceHolder holder = surfaceview. getHolder (); // Get holder

Holder. addCallback (this); // Add holder to the callback Interface

// SetType must be set to avoid errors.

Holder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );

}

 

Class TestVideoListener implements OnClickListener {

 

@ Override

Public void onClick (View v ){

If (v = start ){

Mediarecorder = new MediaRecorder (); // create a mediarecorder object

// Set the recorded video source to Camera (Camera)

Mediarecorder. setVideoSource (MediaRecorder. VideoSource. CAMERA );

// Set the video Encapsulation Format THREE_GPP to 3gp. MPEG_4 to mp4 after recording.

Mediarecorder

. SetOutputFormat (MediaRecorder. OutputFormat. THREE_GPP );

// Sets the recorded video encoding h263 h264

Mediarecorder. setVideoEncoder (MediaRecorder. VideoEncoder. H264 );

// Set the video recording resolution. It must be placed behind the encoding and format. Otherwise, an error is returned.

Mediarecorder. setVideoSize (176,144 );

// Set the Frame Rate of the recorded video. It must be placed behind the encoding and format. Otherwise, an error is returned.

Mediarecorder. setVideoFrameRate (20 );

Mediarecorder. setPreviewDisplay (surfaceHolder. getSurface ());

// Set the output path of the video file

Mediarecorder. setOutputFile ("/sdcard/love.3gp ");

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 ();

}

}

If (v = stop ){

If (mediarecorder! = Null ){

// Stop recording

Mediarecorder. stop ();

// Release resources

Mediarecorder. release ();

Mediarecorder = null;

}

}

 

}

 

}

 

@ Override

Public void surfaceChanged (SurfaceHolder holder, int format, int width,

Int height ){

// Assign the holder, which is the holder that is obtained in oncreat, to surfaceHolder.

SurfaceHolder = holder;

}

 

@ Override

Public void surfaceCreated (SurfaceHolder holder ){

// Assign the holder, which is the holder that is obtained in oncreat, to surfaceHolder.

SurfaceHolder = holder;

}

 

@ Override

Public void surfaceDestroyed (SurfaceHolder holder ){

// Set surfaceDestroyed to null at the same time.

Surfaceview = null;

SurfaceHolder = null;

Mediarecorder = null;

}

}

 

Main. xml

 

 

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "horizontal">

 

<LinearLayout

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: layout_weight = "1">

 

<SurfaceView

Android: id = "@ + id/surfaceview"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"/>

</LinearLayout>

 

<LinearLayout

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: layout_weight = "4"

Android: gravity = "center"

Android: orientation = "vertical">

 

<Button

Android: id = "@ + id/start"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: text = "Start"/>

 

<Button

Android: id = "@ + id/stop"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: text = "Stop"/>

</LinearLayout>

 

</LinearLayout>

 

AndroidManifest. xml

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"

Package = "com. video"

Android: versionCode = "1"

Android: versionName = "1.0" type = "codeph" text = "/codeph">

 

<Application

Android: icon = "@ drawable/icon"

Android: label = "@ string/app_name">

<Activity

Android: label = "@ string/app_name"

Android: name = ". TestBasicVideo">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

 

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

</Application>

 

<Uses-sdk android: minSdkVersion = "8"/>

 

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

</Uses-permission>

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

</Uses-permission>

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

</Uses-permission>

 

</Manifest>

 

Android learning and sharing from zookeeper

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.