Video Recording on Android

Source: Internet
Author: User

Despite the fact that the Google SDK documentation is related, the content is too short and concise, because you need to write a simple demo, so I learned a bit.

Now let's take a look at how to write:

 

First, we must use the camera, so we need to declare the permission in the manifest file:

<Uses-Permission Android: Name = "android. Permission. Camera"/>
If you want to use some hardware properties, make additional declarations, such:

<Uses-feature Android: Name = "android. Hardware. Camera"/>
<Uses-feature: Name = "android. Hardware. Camera. autofocus"/>
Of course, if you want to record audio, you must add:

<Uses-Permission Android: Name = "android. Permission. record_audio"/>

When recording, we usually want to see what we are shooting. This requires preview, and preview needs to be implemented on a surfaceview. Therefore, we need to add a control:

<Surfaceview Android: Id = "@ + ID/surface_camera" xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_centerinparent = "true"
Android: layout_weight = "1">
</Surfaceview>
Then, let's implement this activity:

Public class customvideocamera extends activity implements surfaceholder. Callback {

Private Static final string tag = "camera_tutorial ";

Private surfaceview;
Private surfaceholder;
Private camera;
Private Boolean previewrunning;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. camera_surface );
Surfaceview = (surfaceview) findviewbyid (R. Id. surface_camera );
Surfaceholder = surfaceview. getholder ();
Surfaceholder. addcallback (this );
Surfaceholder. settype (surfaceholder. surface_type_push_buffers );
}

@ Override
Public void surfacecreated (surfaceholder holder ){
Camera = camera. open ();
If (camera! = NULL ){
Camera. Parameters Params = camera. getparameters ();
Camera. setparameters (Params );
}
Else {
Toast. maketext (getapplicationcontext (), "camera not available! ", Toast. length_long). Show ();
Finish ();
}
}

@ Override
Public void surfacechanged (surfaceholder holder, int format, int width, int height ){
If (previewrunning ){
Camera. stoppreview ();
}
Camera. parameters P = camera. getparameters ();
P. setpreviewsize (width, height );
P. setpreviewformat (pixelformat. JPEG );
Camera. setparameters (P );

Try {
Camera. setpreviewdisplay (holder );
Camera. startpreview ();
Previewrunning = true;
}
Catch (ioexception e ){
Log. E (TAG, E. getmessage ());
E. printstacktrace ();
}
}

@ Override
Public void surfacedestroyed (surfaceholder holder ){
Camera. stoppreview ();
Previewrunning = false;
Camera. Release ();
}
}
Then, we can record:

Private mediarecorder;
Private Final int maxdurationinms = 20000;
Private Final long maxfilesizeinbytes = 500000;
Private Final int videoframespersecond = 20;

Public Boolean startrecording (){
Try {
Camera. Unlock ();

Mediarecorder = new mediarecorder ();

Mediarecorder. setcamera (CAMERA );
Mediarecorder. setaudiosource (mediarecorder. audiosource. Mic );
Mediarecorder. setvideosource (mediarecorder. videosource. Camera );

Mediarecorder. setoutputformat (mediarecorder. outputformat. Default );

Mediarecorder. setmaxduration (maxdurationinms );

Tempfile = new file (getcachedir (), cachefilename );
Mediarecorder. setoutputfile (tempfile. getpath ());

Mediarecorder. setvideoframerate (videoframespersecond );
Mediarecorder. setvideosize (surfaceview. getwidth (), surfaceview. getheight ());

Mediarecorder. setaudioencoder (mediarecorder. audioencoder. Default );
Mediarecorder. setvideoencoder (mediarecorder. videoencoder. Default );

Mediarecorder. setpreviewdisplay (surfaceholder. getsurface ());

Mediarecorder. setmaxfilesize (maxfilesizeinbytes );

Mediarecorder. Prepare ();

Mediarecorder. Start ();

Return true;
} Catch (illegalstateexception e ){
Log. E (TAG, E. getmessage ());
E. printstacktrace ();
Return false;
} Catch (ioexception e ){
Log. E (TAG, E. getmessage ());
E. printstacktrace ();
Return false;
}
}
Stop shooting, then:

Public void stoprecording (){
Mediarecorder. Stop ();
Camera. Lock ();
}

 

In this way, after compilation, it can be implemented on Android OS 2.1 and 2.2, but there will be some minor problems on some machines, mainly in the preview, the preview in the app is done by another thread. I wonder if it will improve?

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.