Android's Audiorecord recording implementation

Source: Internet
Author: User

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

Related Article

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.