Class recordthread extends thread {
Private audiorecord Ar;
Private int BS;
Private Final int sample_rate_in_hz= 8000;
Private Boolean isrun = false;
Public recordthread (){
Super ();
BS = audiorecord. getminbuffersize (sample_rate_in_hz,
Audioformat. channel_configuration_mono,
Audioformat. encoding_pcm_16bit );
AR = new audiorecord (mediarecorder. audiosource. Mic, sample_rate_in_hz,
Audioformat. channel_configuration_mono,
Audioformat. encoding_pcm_16bit, BS );
}
Public void run (){
Super. Run ();
Ar. startrecording ();
//
Short []
Buffer = new short [BS];
Isrun = true;
While (isrun ){
Int r = ar. Read (buffer, 0, BS );
Int V = 0;
// Extracts the buffer content and calculates the sum of squares.
For (INT I = 0; I <buffer. length; I ++ ){
// There Is No optimization for the operation here, in order to display the code more clearly
V + = buffer <I> * buffer <I>;
}
// The value of the value is controlled between 0 and 100. 0 is the smallest. "= 100 is the maximum !!
Int value = (INT) (math. Abs (INT) (V/(float) R)/10000)> 1 );
Log. D ("111", "V =" + V );
// Divide the sum of squares by the total length of the data to obtain the volume. You can obtain the white noise value and then standardize the actual sampling.
// If you want to use this value for operations, we recommend that you use sendmessage to throw it and process it in handler.
Log. D ("222", String. valueof (V/(float) R ));
Double DB = 10 * Math. log10 (V/(double) R );
Log. D ("333", "DB =" + dB );
Message MSG = new message ();
MSG. What = 3;
MSG. arg1 = value;
Mhandler. sendmessage (MSG );
}
Ar. Stop ();
}
Public void pause (){
// Call in onpause of the activity that calls this thread to release the microphone when the activity is paused
Isrun = false;
}
Public void start (){
// Call in onresume of the activity that calls this thread so that the activity resumes obtaining the microphone input volume
If (! Isrun ){
Super. Start ();
}
}
} </I>