0

I am very new to android development and I don't have enough experience. So the question I am asking might be very simple. I want to detect if there is noise in the environment using microphone. Now if there is no mic on the cellphone I will toast a relevant.

I found the code from here: android: Detect sound level

On main activity I have a button. Pressing the button will toast some result. But I only get 0.0 even though there is a noise in the room. Could some one give me some hint on this please.

public class MainActivity extends AppCompatActivity implements SensorEventListener {

double soundLevel;
protected void onCreate(Bundle savedInstanceState) {

noiseButton = findViewById(R.id.noiseCheck);
        PackageManager PM= this.getPackageManager();
        final boolean microphone = PM.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
        noiseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (microphone){
                    double soundLevel =  detectEnvironmentalNoise();
                    Toast.makeText(mContext, "Environmental noise level is " + soundLevel , Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(mContext, "This device is not equipped to microphone to detect environmental noise", Toast.LENGTH_LONG).show();
                }
            }
        });

   }

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

            audio.startRecording();
            short[] buffer = new short[100];

            int bufferReadResult = 1;

            if (audio != null) {

                // Sense the voice...
                bufferReadResult = audio.read(buffer, 0, 10000);
                double sumLevel = 0;
                for (int i = 0; i < bufferReadResult; i++) {
                    sumLevel += buffer[i];
                }
            lastLevel = Math.abs((sumLevel / bufferReadResult));
       }
       return lastLevel;
    }
}
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
user1836957
  • 427
  • 2
  • 9
  • 24

1 Answers1

0

You need to implement getMaxAmplitude() for AudioRecord as described in below post:

Implement getMaxAmplitude for audioRecord

Or you can use MediaRecorder as below:

public class SoundMeter {

    private MediaRecorder mRecorder = null;

    public void start() {
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile("/dev/null"); 
            mRecorder.prepare();
            mRecorder.start();
        }
    }

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

    public double getAmplitude() {
        if (mRecorder != null)
            return  mRecorder.getMaxAmplitude();
        else
            return 0;
    }
}
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126