Objective:
In fact, recording in Android can be recorded with Mediarecord, the operation is relatively simple. However, the audio cannot be processed. Consider that the project is done in real-time voice can only select Audiorecord for recording.
This article is a review of Audiorecord:
Public classAudiorecordmanager { Public Static FinalString TAG = "Audiorecordmanager"; PrivateAudiorecord Mrecorder; PrivateDataOutputStream dos; PrivateThread Recordthread; Private BooleanIsstart =false; Private StaticAudiorecordmanager minstance; Private intbuffersize; PublicAudiorecordmanager () {buffersize= Audiorecord.getminbuffersize (8000, Audioformat.channel_in_mono, audioformat.encoding_pcm_16bit); Mrecorder=NewAudiorecord (audiosource.mic, 8000, Audioformat.channel_in_mono, Audioformat.encoding_pcm_16bit, BufferSize * 2); } /*** Get a singleton reference * *@return */ Public StaticAudiorecordmanager getinstance () {if(Minstance = =NULL) { synchronized(Audiorecordmanager.class) { if(Minstance = =NULL) {minstance=NewAudiorecordmanager (); } } } returnminstance; } /*** Destroying threading methods*/ Private voidDestroythread () {Try{Isstart=false; if(NULL! = Recordthread && Thread.State.RUNNABLE = =recordthread.getstate ()) { Try{Thread.Sleep (500); Recordthread.interrupt (); } Catch(Exception e) {recordthread=NULL; }} recordthread=NULL; } Catch(Exception e) {e.printstacktrace (); } finally{Recordthread=NULL; } } /*** Start the recording thread*/ Private voidStartthread () {destroythread (); Isstart=true; if(Recordthread = =NULL) {Recordthread=NewThread (recordrunnable); Recordthread.start (); } } /*** Recording Thread*/Runnable recordrunnable=NewRunnable () {@Override Public voidrun () {Try{android.os.Process.setThreadPriority (Android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); intBytesrecord; //int buffersize =; byte[] Tempbuffer =New byte[buffersize]; if(Mrecorder.getstate ()! =audiorecord.state_initialized) {Stoprecord (); return; } mrecorder.startrecording (); //Writetofilehead (); while(Isstart) {if(NULL!=Mrecorder) {Bytesrecord= Mrecorder.read (tempbuffer, 0, buffersize); if(Bytesrecord = = Audiorecord.error_invalid_operation | | bytesrecord = =audiorecord.error_bad_value) { Continue; } if(Bytesrecord! = 0 && Bytesrecord! =-1) { //in this case, the recorded audio data can be processed two times such as voice changer, compression, noise reduction, gain and other operations//we directly write the PCM audio raw data to the file here can be sent directly to the server each other using Audiotrack to play the original dataDos.write (tempbuffer, 0, Bytesrecord); } Else { Break; } } } } Catch(Exception e) {e.printstacktrace (); } } }; /*** Save File * *@paramPath *@throwsException*/ Private voidSetPath (String Path)throwsException {File file=NewFile (path); if(File.exists ()) {file.delete (); } file.createnewfile (); DOS=NewDataOutputStream (NewFileOutputStream (file,true)); } /*** Start Recording * *@paramPath*/ Public voidStartrecord (String path) {Try{setpath (path); Startthread (); } Catch(Exception e) {e.printstacktrace (); } } /*** Stop Recording*/ Public voidStoprecord () {Try{destroythread (); if(Mrecorder! =NULL) { if(mrecorder.getstate () = =audiorecord.state_initialized) {mrecorder.stop (); } if(Mrecorder! =NULL) {mrecorder.release (); } } if(Dos! =NULL) {Dos.flush (); Dos.close (); } } Catch(Exception e) {e.printstacktrace (); } }}
Android's Audiorecord recording implementation