Friday, July 1, 2011

[android-developers] Detecting the current volume on AudioRecord

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:

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

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment