Android recording to obtain the Real-Time microphone volume)-Materials

Source: Internet
Author: User

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 ();}}}

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.