Android skirt blowing implementation code

Source: Internet
Author: User

Today, we will bring you a game in Android. The core implementation code for blowing skirts is as follows: package com. xxiyy. spl; 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. getMinBufferSi Ze (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 (); // buffer byte [] buffer = new byte [bs]; isRun = true; while (isRun) {int r = ar. read (buffer, 0, Bs); int v = 0; // extracts the buffer content and performs 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 () ;}} mentioned in the original article "sum of squares divided by the total length of data to get the volume size". Some Articles mentioned that the volume value varies with mobile phones, the same voice, but the output value is quite different. Then there are some algorithms for adjusting the "volume" through some calculations, two of which are: 1. Calculating the noise and adjusting the volume: www.2cto. the value of comvalue 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); 2. Calculate the decibel value: the value should be the amplitude of the sound, it is not the volume size. The sound size should be measured in decibels. double dB = 10 * Math. log10 (v/(double) r );

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.