Http://www.mikespook.com/index.php/archives/762
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. Many people on the Internet have asked how to deal with this problem, but there are still some answers, but there is no actual code. After a simple exploration, I wrote a demo and tried it. Share it with you.
I don't want to explain the code. Let's look at the comments.
import android.media.AudioFormat; |
import android.media.AudioRecord; |
import android.media.MediaRecorder; |
public class RecordThread extends Thread { |
private static int SAMPLE_RATE_IN_HZ = 8000 ; |
private boolean isRun = false ; |
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); |
// Buffer used for reading |
byte [] buffer = new byte [bs]; |
int r = ar.read(buffer, 0 , bs); |
// 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]; |
// 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)); |
// Call in onpause of the activity that calls this thread to release the microphone when the activity is paused |
// Call in onresume of the activity that calls this thread so that the activity resumes obtaining the microphone input volume |
This entry was posted on Monday, November 8th, 2010 at 20:02 and is filed under android. you can follow any responses to this entry through the RSS 2.0 feed. you can leave a response, or trackback from your own