Custom Video Recorder example, nothing new
Package Com.example.ggpla.myapplication;import Android.hardware.camera;import Android.media.CamcorderProfile; Import Android.media.mediarecorder;import Android.os.bundle;import android.support.v7.app.AppCompatActivity; Import Android.util.log;import Android.view.surfaceholder;import Android.view.surfaceview;import Android.widget.compoundbutton;import Android.widget.toast;import Android.widget.togglebutton;import Com.example.ggpla.utils.fileoptionutils;import Java.io.ioexception;public class Myvideocamera extends Appcompatactivity implements surfaceholder.callback{private Camera Mycamera; Private Surfaceview Mysview; Private Surfaceholder Myholder; Private ToggleButton ToggleButton; Private Boolean misrecording = false; Private Mediarecorder Mediarecorder; Private String Videourl; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_MAIN22); Mysview = (SurfacevieW) Findviewbyid (R.ID.SURFACEVIEW2); Myholder = Mysview.getholder (); Myholder.addcallback (this); Myholder.settype (surfaceholder.surface_type_push_buffers); ToggleButton = (ToggleButton) Findviewbyid (R.id.togglebutton2); Togglebutton.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override Pub LIC void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {if (isChecked) {//true, from off state to open State Startmediarecorder (); Toast.maketext (Myvideocamera.this, "Start recording", Toast.length_long). Show (); } else {//false, from open state to Off state Stopmediarecorder (); Toast.maketext (Myvideocamera.this, "video complete", Toast.length_long). Show (); } } }); } @Override protected void Onresume () {super.onresume (); Opencamera (); if (Myholder! = null) {Setstartpreview (myholder,myCamera); }} @Override protected void OnPause () {super.onpause (); Releasecamera (); }/** * Gets the Camera hardware object */private void Opencamera () {if (Mycamera = = null) {Mycamera = camera. Open (Camera.CameraInfo.CAMERA_FACING_BACK); }}/** * Release camera resource */private void Releasecamera () {if (Mycamera! = null) {Mycamera.setpre Viewcallback (NULL); Mycamera.stoppreview (); Mycamera.release (); Mycamera = null; }}//Start private void Setstartpreview (Surfaceholder surfaceholder,camera camera) {try {camera. Setpreviewdisplay (Surfaceholder); Set the system preview angle to horizontal screen camera.setdisplayorientation (90); Camera.startpreview (); } catch (IOException e) {e.printstacktrace (); }} @Override public void surfacecreated (Surfaceholder holder) {Setstartpreview (Myholder,mycamera); } @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {mycamera.stoppreview (); Setstartpreview (Myholder,mycamera); } @Override public void surfacedestroyed (Surfaceholder holder) {Releasecamera (); }/** * End Recording method */private void Stopmediarecorder () {if (mediarecorder!=null) {if (Misrecord ing) {mediarecorder.stop (); Mcamera.lock (); Mediarecorder.reset (); Mediarecorder.release (); Mediarecorder=null; Misrecording = false; try {mycamera.reconnect (); } catch (IOException e) {Toast.maketext (this, "Reconect fail", Toast.length_long). Show (); E.printstacktrace (); }}}}/** * Start recording method */private void Startmediarecorder () {//Custom static method, http://87 79947.blog.51cto.com/8769947/1733467 has introduced the Fileoptionutils tool class Videourl = Fileoptionutils.getsavepath (fileoptionutils.named_by_self, "test", "M P4 "," VIDEO "); Mycamera.unlock (); Misrecording = true; Mediarecorder = new Mediarecorder (); Mediarecorder.reset (); Mediarecorder.setcamera (Mycamera); Mediarecorder.setaudiosource (MediaRecorder.AudioSource.DEFAULT); Mediarecorder.setvideosource (MediaRecorder.VideoSource.CAMERA); Set the recorder shooting parameters, with the following parameters can only be set to two Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.MPEG_4); Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); Mediarecorder.setvideoencoder (MediaRecorder.VideoEncoder.H264); Mediarecorder.setvideoframerate (5); Mediarecorder.setvideosize (640, 480);//Set the recorder shooting parameters, with the following parameters can only two select one, Camcorderprofile.quality_high for shooting quality. Camcorderprofile mcamcorderprofile = Camcorderprofile.get (Camera.CameraInfo.CAMERA_FACING_BACK, Camcorderprofile . Quality_high);//MediarecordEr.setprofile (Mcamcorderprofile); Mediarecorder.setoutputfile (Videourl); Mediarecorder.setpreviewdisplay (Myholder.getsurface ()); try {mediarecorder.prepare (); } catch (Exception e) {misrecording = false; Toast.maketext (Myvideocamera.this, "fail", Toast.length_long). Show (); E.printstacktrace (); Mycamera.lock (); } mediarecorder.start (); }}
This article is from the "Do Not Laugh" blog, please make sure to keep this source http://8779947.blog.51cto.com/8769947/1733471
Custom video recorder without any new ideas