Reference article Android Real-time fetch volume decibel value detailed: http://www.jb51.net/article/64806.htm
public class Mediarecorderdemo {private final String TAG = "Mediarecord";
Private Mediarecorder Mmediarecorder;
public static final int max_length = 1000 * 60 * 10;//maximum recording length 1000*60*10;
Private String FilePath;
Public Mediarecorderdemo () {This.filepath = "/dev/null";
Public Mediarecorderdemo (file file) {This.filepath = File.getabsolutepath ();
Private long starttime;
Private long Endtime; /** * Start recording using AMR format * * Audio file * @return/public void Startrecord () {//Start recording/*①ini
Tial: Instantiate Mediarecorder Object */if (Mmediarecorder = null) Mmediarecorder = new Mediarecorder (); try {/*②setaudiosource/setvediosource */Mmediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//Set Set the microphone/*② the encoding of the audio file: Aac/amr_nb/amr_mb/default (waveform) Sampling/Mmediarecorder.setoutputformat (MEDIARECORDER.OUTP
Utformat.default); /* *② format of output file: Three_gpp/mpeg-4/raw_amr/default THREE_GPP (3gp format *, H263 video/arm audio encoding), MPEG-4, RAW_AMR (audio only supported and audio coding required for AMR_NB)/Mmediarecord
Er.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
/*③ Preparation * * * Mmediarecorder.setoutputfile (FilePath);
Mmediarecorder.setmaxduration (max_length);
Mmediarecorder.prepare ();
/*④ Start * * Mmediarecorder.start ();
Audiorecord Audiorecord.
/* Get start time * */starttime = System.currenttimemillis ();
Updatemicstatus ();
LOG.I ("Action_start", "starttime" + starttime);
catch (IllegalStateException e) {log.i (TAG, "Call Startamr (File mrecaudiofile) failed!"
+ e.getmessage ());
catch (IOException e) {log.i (TAG, "Call Startamr (File mrecaudiofile) failed!"
+ e.getmessage ());
}/** * Stop recording */public long Stoprecord () {if (Mmediarecorder = null) return 0L; Endtime = System.currenttImemillis ();
LOG.I ("Action_end", "Endtime" + endtime);
Mmediarecorder.stop ();
Mmediarecorder.reset ();
Mmediarecorder.release ();
Mmediarecorder = null;
LOG.I ("Action_length", "Time" + (endtime-starttime));
return endtime-starttime;
Private final Handler Mhandler = new Handler ();
Private Runnable Mupdatemicstatustimer = new Runnable () {public void run () {updatemicstatus ();
}
};
/** * Update Microphone status */private int BASE = 1; private int space = 100;//interval sampling time private void Updatemicstatus () {if (Mmediarecorder!= null) {double R
Atio = (double) mmediarecorder.getmaxamplitude ()/base;
Double db = 0;//db if (ratio > 1) db = * MATH.LOG10 (ratio);
LOG.D (TAG, "decibel value:" +db);
Mhandler.postdelayed (Mupdatemicstatustimer, space); }
}
}