Android has some interesting applications, such as blowing skirts and blowing balloons. The microphone input volume is obtained in real time and then processed accordingly. How can we obtain the sound size during the recording process? There is an instance below
Java code:
Package EOE. demo; import android. media. audioformat; import android. media. audiorecord; import android. media. mediarecorder; import android. util. log; public class recordthread extends thread {private audiorecord Ar; private int BS; Private Static int sample_rate_in_hz = 8000; private Boolean isrun = false; Public recordthread () {super (); BS = audiorecord. getminbuffersize (sample_rate_in_hz, audioformat. channel_co Nfiguration_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 (); // bufferbyte [] buffer = new byte [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 (INT ). I = 0; I <buffer. length; I ++) {// No optimization is performed here. To better demonstrate the code v + = buffer [I] * buffer [I];} // 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 ("SPL", String. valueof (V/(float) R);} ar. stop ();} public void pause () {// call in onpause of the activity that calls this thread so that the microphone isrun = false is released when the activity is paused;} 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 ();}}}