1, because of the compatibility of the API, Mediarecorder, the start of the way is also different, because I watch the video is API 8 so can not start, in the Internet to check a lot of information is not clear, and ultimately to the official website to view to achieve:
The way the official website is implemented:
I realized the demo, hoping to give me the same beginner a little help, less detours:
Permissions Required:
<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-feature android:name= "Android.hardware.camera"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
Main_xml file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android . Com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orientatio n= "vertical" tools:context= "com.example.mycamera.mainactivity$placeholderfragment" > <surfaceview android:layout_weight= "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " Android:id= "@+id/camera"/> <relativelayout android:layout_weight= "0" Android:layout_w Idth= "Fill_parent" android:layout_height= "wrap_content" ><button android:layout_width= "Wrap_conten T "android:layout_height=" wrap_content "android:text=" to start recording "android:layout_alignparentright=" true " Android:id= "@+id/pz" android:onclick= "Luxiang"/><button android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:text= "Stop" android:id= "@+id/dj" android:layout_toleftof= "@+id/pz" Andro id:onclick= "Tingzhi"/></relativelayout></linearlayout>
public class Mainactivity extends Actionbaractivity {//actionbaractivityprivate Surfaceview sv;private SurfaceHolder Holder;private Camera mcamera;private mediarecorder recorder; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// Remove the InfoBar Setcontentview (R.layout.activity_main); sv= (Surfaceview) Findviewbyid (R.id.camera); Holder=sv.getholder (); Holder.addcallback (New Mycallback ()); public void Tingzhi (View v) {recorder.release (); Recorder=null; } public void Luxiang (View v) throws IOException {recorder=new mediarecorder (); Recorder.reset (); Mcamera.unlock (); Recorder.setcamera (Mcamera); Recorder.setaudiosource (MediaRecorder.AudioSource.CAMCORDER);//Set audio source camera Recorder.setVideoSource (MediaRecorder.VideoSource.CAMERA); Recorder.setprofile (Camcorderprofile.get (Camcorderprofile.quality_high));//Specifies the quality of the recording recorder.setoutputfile ("Data /data/musics/hahagaga.3gp "); 5th step: Specify preview Output Recorder.setpreviewdisplay (Holder.getsurface ()); try {recorder.prepare (); } catch (Exception e) {recorder.release (); } recorder.start (); Recorder=new Mediarecorder ();//Recorder.reset ();//Recorder.setvideosource (MediaRecorder.VideoSource.CAMERA); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC);//Recorder.setoutputformat ( MediaRecorder.OutputFormat.THREE_GPP);////video-size-values=640x480,576x432,480x320,432x240,384x288,352x288, 320x240,240x160,176x144////preview-size-values=640x480,576x432,480x320,432x240,384x288,352x288,320x240,240x160 , 176x144//Recorder.setvideosize (576, 432);//Recorder.setvideoframerate (5);//Recorder.setvideoencoder (MediaR Ecorder. videoencoder.h264);Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);//Recorder.setoutputfile ("data/data/musics/ HAHAGAGA.3GP ");//Recorder.setpreviewdisplay (Holder.getsurface ());//try{//Recorder.prepare ();//}catch (EXCEP tion e)//{//System.out.println ("Preparation failed");//E.printstacktrace ();//}//try{//Recorder.start ();//}ca TCH (Exception e)//{//System.out.println ("Start failed");//E.printstacktrace ();/}} class Mypicturecallb ACK implements Picturecallback {@Overridepublic void Onpicturetaken (byte[] data, camera camera) {//dada is a compressed file after taking photos Image=new File (Environment.getdatadirectory (), "zp.jpg"); FileOutputStream out=null;try {out=new fileoutputstream (image); Out.write (data); Out.flush (); Out.close ();} catch ( Exception e) {e.printstacktrace (); Toast.maketext (Mainactivity.this, "Save photo failed! ", Toast.length_long). Show ();} Finally{//mcamera.startpreview ();}} } class Mycallback implements callback{@Overridepublic void surfacecreated(Surfaceholder Holder) {//video mcamera=camera.open (); try {mcamera.setpreviewdisplay (holder);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Mcamera.startpreview ();} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width,int height) {} @Overridepublic void Surfac Edestroyed (Surfaceholder holder) {//mcamera.release ();//mcamera=null;}}}
Mediarecorder study, android4.0 after Mediarecorder start failed reason