Android recording and getting volume

Source: Internet
Author: User

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>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.