title: Detecting the current volume on Android
body: <p>I'm building an application that is always recording audio from the microphone, whenever the audio reaches a certain threshold I perform a certain action.</p>
  
  <p>However how should I calc the appropriate volume for the threshold ? I've a static volume coded which works well across some devices but not all devices (in somes cases it is too sensitive or vice versa).</p>
  
  <p>I'm using AudioRecord, here's part of the code:</p>
  
  <pre><code>int bufferSize = AudioRecord.getMinBufferSize(Constants.RECORDING_FREQUENCY,Constants.channelConfiguration,Constants.audioEncoding);    
  AudioRecord audioRecord = new AudioRecord( MediaRecorder.AudioSource.MIC,Constants.RECORDING_FREQUENCY,Constants.channelConfiguration,Constants.audioEncoding, bufferSize); 
  short[] buffer = new short[bufferSize];
  while(true) {
     int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
     for (int i = 0; i &lt; bufferReadResult; i++) {
        currentVolume = java.lang.Math.abs(buffer[i]);
        if (currentVolume &gt; Constants.NO_VOLUME_AMPLITUDE)
          // ALRIGHT ! This is what I'm looking for :D
     }
  }
  </code></pre>
  
  <p>So, my question is: how do I calculate Constants.NO_VOLUME_AMPLITUDE instead of having it hard coded ?</p>
  
  <p>Thanks so much in advance,
  Ze</p>