Home » Android » audio – android: Detect sound level

audio – android: Detect sound level

Posted by: admin April 23, 2020 Leave a comment


Using MediaRecorder I capture sound from device’s microphone. From the sound I get I need only to analyze the sound volume (sound loudness), without saving the sound to a file.

Two questions:

  1. How do I get the loudness for the sound at a given moment in time?
  2. How do I do the analyze without saving the sound to a file?

Thank you.

How to&Answers:
  1. Use mRecorder.getMaxAmplitude();

  2. For the analysis of sound without saving all you need is use mRecorder.setOutputFile("/dev/null");

Here´s an example, I hope this helps

public class SoundMeter {

    private MediaRecorder mRecorder = null;

    public void start() {
            if (mRecorder == null) {
                    mRecorder = new MediaRecorder();

    public void stop() {
            if (mRecorder != null) {
                    mRecorder = null;

    public double getAmplitude() {
            if (mRecorder != null)
                    return  mRecorder.getMaxAmplitude();
                    return 0;



If you want to analyse a sample of sound taken directly from the microphone without saving the data in a file, you need to make use of the AudioRecord Object as follows:

int sampleRate = 8000;
try {
    bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO,
    audio = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);
} catch (Exception e) {
    android.util.Log.e("TrackingFlow", "Exception", e);

Then you have to start recording when ready:


Now it’s time to start reading samples as follows:

short[] buffer = new short[bufferSize];

    int bufferReadResult = 1;

    if (audio != null) {

        // Sense the voice...
        bufferReadResult = audio.read(buffer, 0, bufferSize);
        double sumLevel = 0;
        for (int i = 0; i < bufferReadResult; i++) {
            sumLevel += buffer[i];
        lastLevel = Math.abs((sumLevel / bufferReadResult));

The last code combines all the different samples amplitudes and assigns the average to the lastLeveL variable, for more details you can go to this post.



I played around with a few sound recording source code apps but it took a while to get it.

You can copy Google’s NoiseAlert source code – the SoundMeter code, which is what I’ve used.

getMaxAmplitude ranges from 0 to 32768. The EMA stuff I didn’t bother using.

In your main activity, you have to declare a MediaRecorder object and call its start() function as Anna quoted.

For more info, you can refer to my project:
Where I needed to send the value of the sound amplitude over BlueTooth.

Exact code is here: https://github.com/garytse89/Amplify/tree/master/Android%20Code/src/com/garytse89/allin

Just skim through the parts that have anything to do with the MediaRecorder object associated.