Package Com.k1.doctor.media;import Java.io.file;import java.io.ioexception;import java.util.uuid;import Android.media.mediarecorder;import Android.os.environment;import Android.os.Handler; Public classAudiomanager {PrivateMediarecorder Mrecorder; PrivateString mdirstring; PrivateString mcurrentfilepathstring; PrivateBoolean isprepared;//Are you ready for this ? /** * Singleton Method 1 First declare a static type of variable a 2 in the declaration of the default constructor 3 again with public synchronized static * class name getinstance () {if (a==null) { A=new class ();} return A; } or use the following method*/ /** * Single instance of this class*/ Private StaticAudiomanager minstance; PrivateAudiomanager (String dir) {mdirstring=dir; } Public StaticAudiomanager getinstance () {if(Minstance = =NULL) {synchronized (Audiomanager.class) { if(Minstance = =NULL) {minstance=NewAudiomanager (GetPath ()); } } } returnminstance; } /** * callback function, ready to finish, button will start to display the recording box * * @author nickming **/ Public InterfaceAudiostagelistener {voidwellprepared (); } PublicAudiostagelistener Mlistener; Public voidSetonaudiostagelistener (Audiostagelistener listener) {Mlistener=Listener; } //Preparation Methods Public voidPrepareaudio () {Try { //it should be false at first.isprepared =false; File dir=NewFile (mdirstring); if(!dir.exists ()) {Dir.mkdirs (); } String filenamestring=Generalfilename (); File File=NewFile (dir, filenamestring); Mcurrentfilepathstring=File.getabsolutepath (); Mrecorder=NewMediarecorder (); //Set Output FileMrecorder.setoutputfile (File.getabsolutepath ()); //Set the Meidarecorder audio source to be a microphoneMrecorder.setaudiosource (MediaRecorder.AudioSource.MIC); //set the output format for file audio to AMRMrecorder.setoutputformat (MediaRecorder.OutputFormat.RAW_AMR); //set the encoding format for audio to AMRMrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); //strictly follow the Mediarecorder state flowchart given by Google's official APIMrecorder.prepare (); Mrecorder.start (); NewThread (NewRunnable () {@Override Public voidrun () {inti =0; while(I < -&& Mrecorder! =NULL) {i++; Try{Thread.Sleep ( +); } Catch(interruptedexception e) {e.printstacktrace (); }} mhandler.sendemptymessage (0); }}). Start (); //ready to finish .isprepared =true; //It 's ready to be recorded. if(Mlistener! =NULL) {mlistener.wellprepared (); } } Catch(IllegalStateException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } /** * The name of the randomly generated file * * @return*/ PrivateString Generalfilename () {//TODO auto-generated Method Stub returnUuid.randomuuid (). toString () +". Amr"; } //Level to get sound Public intGetvoicelevel (intmaxlevel) { //Mrecorder.getmaxamplitude () This is the amplitude range of the audio, the domain is 1-32767 if(isprepared) {Try { //forensics +1, or less than 7. returnMaxlevel * Mrecorder.getmaxamplitude ()/32768+1; } Catch(Exception e) {//TODO auto-generated Catch block } } return 1; } //Freeing Resources Public voidrelease () {//strictly follow the API processMrecorder.stop (); Mrecorder.release (); Mrecorder=NULL; } //Cancel because a file was generated when prepare, so the Cancel method should delete the file,//This is the difference from the release method Public voidCancel () {release (); if(Mcurrentfilepathstring! =NULL) {File file=NewFile (mcurrentfilepathstring); File.delete (); Mcurrentfilepathstring=NULL; } } PublicString Getcurrentfilepath () {//TODO auto-generated Method Stub returnmcurrentfilepathstring; } Public StaticString GetPath () {File Sddir=NULL; Boolean sdcardexist= Environment.getexternalstoragestate (). Equals (environment.media_mounted);//determine if the SD card exists if(sdcardexist) {Sddir= Environment.getexternalstoragedirectory ();//Get with directory } Else{Sddir=environment.getdatadirectory (); } returnSddir.tostring () +"/voice"; } Handler Mhandler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {if(isprepared && Mrecorder! =NULL) {release (); } }; };}
Audiomanager (recording)