• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Detecting the current volume on Android

zemariamm

Lurker
Feb 18, 2011
5
0
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.

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).

I'm using AudioRecord, here's part of the code:

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 < bufferReadResult; i++) {
      currentVolume = java.lang.Math.abs(buffer[i]);
      if (currentVolume > Constants.NO_VOLUME_AMPLITUDE)
        // ALRIGHT ! This is what I'm looking for :D
   }
}

So, my question is: how do I calculate Constants.NO_VOLUME_AMPLITUDE instead of having it hard coded ?

Thanks so much in advance, Ze
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones