Android : Detect only blow, not talking or knock (Android)

on Monday, November 10, 2014


In a mobile application necessary to catch a signal when a user is blowing into the microphone. It is important to make talking near the microphone, no catch. And now triggered any knocking or talking near the microphone, and do not blow. Help to modify the code to catch only the blow. The code below:



Timer timer2;
private void getAmplitudeFromMicrophone(){

timer2 = new Timer();
timer2.schedule(new TimerTask() {
@Override
public void run() {

isBlowing();

try {
getActivity().runOnUiThread(new Runnable() {
public void run() {
// update UI here
onBlow();
}
});
} catch (Exception e) {
e.printStackTrace();
}

}
}, 5000, 1000); // 5000 milliseconds before the first run, the interval - 1000 milliseconds.

}

public boolean isBlowing() {
boolean recorder=true;
int minSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);

short[] buffer = new short[minSize];

ar.startRecording();
while(recorder)
{

ar.read(buffer, 0, minSize);

for (short s : buffer)
{
if (s > 20000) {
System.out.println("minSize / signal / if (signal > 20000) = " + s);
}
if (Math.abs(s) > 27000) //DETECT VOLUME (IF I BLOW IN THE MIC) // 27000
{
blow_value=Math.abs(s);
System.out.println("Blow Value="+blow_value);
timer2.cancel();
ar.stop();
recorder=false;

return true;

}

}
}
return false;
}

0 comments:

Post a Comment