Android real-time microphone input volume acquisition code

Source: Internet
Author: User
Tags spl

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.

 

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.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();
                // Buffer used for reading
        byte[] 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.
            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));
        }
        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();
        }
    }
}

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

Related Article

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.