Mediarecorder There's no difference between taping and recording. Set the format of the image
Reference: http://www.jb51.net/article/46182.htm
Instance:
Copy Code code as follows:
<!--grant the program the right to record sound-->
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
<!--grant the program permission to use the camera-->
<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--grant permission to use external storage-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Copy Code code as follows:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivity ">
<surfaceview
Android:id= "@+id/dview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
android:orientation= "Horizontal" >
<button
Android:id= "@+id/record"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "@string/record"/>
<button
Android:id= "@+id/stop"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "@string/stop"/>
</LinearLayout>
</RelativeLayout>
Copy Code code as follows:
Package com.android.xiong.videotest;
Import Java.io.File;
Import android.app.Activity;
Import Android.hardware.Camera;
Import Android.media.MediaRecorder;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.view.Menu;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends activity implements Onclicklistener {
Button record, stop;
System video files
File Viodfile;
Mediarecorder Mrecorder;
Surfaceview that shows the video
Surfaceview Sview;
Record whether recording is in progress
Boolean isrecording = false;
Camera Camera;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Record = (Button) Findviewbyid (R.id.record);
Stop = (Button) Findviewbyid (r.id.stop);
Sview = (Surfaceview) Findviewbyid (R.id.dview);
The Stop button is not available
Stop.setenabled (FALSE);
Set surface do not need to maintain their own buffers
Sview.getholder (). SetType (Surfaceholder.surface_type_push_buffers);
Set resolution
Sview.getholder (). Setfixedsize (320, 280);
Setting this component does not allow the screen to automatically close
Sview.getholder (). Setkeepscreenon (True);
Record.setonclicklistener (this);
Stop.setonclicklistener (this);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.record:
if (! Environment.getexternalstoragestate (). Equals (
environment.media_mounted)) {
Toast.maketext (This, "SD card does not exist, please insert card!") ", Toast.length_short). Show ();
Return
}
try {
Creating MediaPlayer Objects
Mrecorder = new Mediarecorder ();
Mrecorder.reset ();
/* camera = Camera.open ();
Camera.unlock ();
Camera.setdisplayorientation (0);
Mrecorder.setcamera (camera);
Create a video file to save recorded video
Viodfile = new File (Environment.getexternalstoragedirectory ()
. Getcanonicalfile () + "/myvideo.mp4");
if (!viodfile.exists ())
Viodfile.createnewfile ();
Set up sound from microphone collection
Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
Set up a camera to capture the image
Mrecorder.setvideosource (MediaRecorder.VideoSource.CAMERA);
Set the output format for video and audio
Mrecorder.setoutputformat (MediaRecorder.OutputFormat.DEFAULT);
Sets the encoding format for the audio,
Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);
Set Image Encoding format
Mrecorder.setvideoencoder (MediaRecorder.VideoEncoder.DEFAULT);
Mrecorder.setorientationhint (90);
Mrecorder.setvideosize (320, 280);
Mrecorder.setvideoframerate (5);
Mrecorder.setoutputfile (Viodfile.getabsolutepath ());
Specify Surfaceview to preview the video
Mrecorder.setpreviewdisplay (Sview.getholder (). Getsurface ());
Mrecorder.prepare ();
Start recording
Mrecorder.start ();
Make the Record button unavailable
Record.setenabled (FALSE);
Make the Stop button available
Stop.setenabled (TRUE);
IsRecording = true;
catch (Exception e) {
E.printstacktrace ();
}
Break
Case R.id.stop:
If you are recording
if (isrecording) {
Stop Recording
Mrecorder.stop ();
Releasing resources
Mrecorder.release ();
Mrecorder = null;
Make the Record button available
Record.setenabled (TRUE);
Make the Stop button unavailable
Stop.setenabled (FALSE);
}
Break
Default
Break
}
}
}