1. Download our Official Android App: Forums for Android!

Apps Detecting the current volume on Android

Discussion in 'Android Development' started by zemariamm, Jul 1, 2011.

  1. zemariamm

    zemariamm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Feb 18, 2011

    Feb 18, 2011
    5
    0
    5
    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 (Text):
    1.  
    2.  
    3. int bufferSize = AudioRecord.getMinBufferSize(Constants.RECORDING_FREQUENCY,Constants.channelConfiguration,Constants.audioEncoding);    
    4. AudioRecord audioRecord = new AudioRecord( MediaRecorder.AudioSource.MIC,Constants.RECORDING_FREQUENCY,Constants.channelConfiguration,Constants.audioEncoding, bufferSize);
    5. short[] buffer = new short[bufferSize];
    6. while(true) {
    7.    int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
    8.    for (int i = 0; i < bufferReadResult; i++) {
    9.       currentVolume = java.lang.Math.abs(buffer[i]);
    10.       if (currentVolume > Constants.NO_VOLUME_AMPLITUDE)
    11.         // ALRIGHT ! This is what I'm looking for :D
    12.    }
    13. }
    14.  
    15.  
    16.  
    So, my question is: how do I calculate Constants.NO_VOLUME_AMPLITUDE instead of having it hard coded ?

    Thanks so much in advance, Ze
     

    Advertisement

Share This Page

Loading...