I want to activate my sensor when a continuous noise sounds ( Android Studio )

Estefania de la Osa :

I am learning to program and the truth is that I don't have much idea. I am programming an application that consists of an audio sensor and when a noise is heard in the house it is activated. I currently have this code.

Activity DetectNoise

public class DetectNoise {




   // This file is used to record voice
   static final private double EMA_FILTER = 0.6;

   private MediaRecorder mRecorder = null;
   private double mEMA = 0.0;

   public void start() {



       if (mRecorder == null) {


           mRecorder = new MediaRecorder();
           mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
           mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
           mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
           mRecorder.setOutputFile("/dev/null");

           try {
               mRecorder.prepare();
               mRecorder.start();
               mEMA = 0.0;
           } catch (IllegalStateException e) {
               Log.e("Error",e.toString());
               e.printStackTrace();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               Log.e("Error",e.toString());
               e.printStackTrace();
           }

       }
   }



public double getAmplitude() {



       if (mRecorder != null)
           //return  (mRecorder.getMaxAmplitude()/1100.0);


           return   20 * Math.log10(mRecorder.getMaxAmplitude() / 2000);
       else
           return 0;

   }

public double getAmplitudeEMA() {
       double amp = getAmplitude();
       mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
       return mEMA;
   }

Activity One

private int mThreshold;
private static final int POLL_INTERVAL = 300;
mSensor = new DetectNoise();
private Handler mHandler = new Handler();

private Runnable mPollTask = new Runnable() {
       public void run() {

               double amp = mSensor.getAmplitude();
               //Log.i("Noise", "runnable mPollTask");
               updateDisplay("Monitoring Voice...", amp);

               if ((amp > mThreshold)) {
                   callForHelp(amp);
                   //Log.i("Noise", "==== onCreate ===");
               }// Runnable(mPollTask) will again execute after POLL_INTERVAL
               mHandler.postDelayed(mPollTask, POLL_INTERVAL); }


The problem is that with any sound it is activated. I want it to activate only when the sound lasts about 5 or 7 seconds continued.

Can anybody help me? Thank you so much for everything

Icodeeeee :

start init time stamp is null, you can get from the first amplitude to record time stamp, if they are sound in the detection process disappear,you can reset time stamp to null, If the sound persists then the first recorded time stamp will be kept.You only need to determine the time stamp and time stamp records are obtained subtraction judge the sound duration

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=292342&siteId=1