1

I am trying to write a simple little thing that stops an audio file from being played when the user "interrupts" it by talking to the phone. I'm using the SoundMeter class that this person wrote to get the maxAmplitude of the microphone. I've set a threshold to 800 and want to test the return of the microphone amplitude against it constantly (every 50ms).

For some reason, it's hit or miss when I get the "interrupt" to show up and when the audio stops. I'd like for the interrupt to be showing the entire time the amplitude is above 800.

    handler = new Handler();
    final Runnable r = new Runnable() {
        @Override
        public void run() {
            mic_amplitude.setText(""+mSoundMeter.getAmplitude());
            if(mSoundMeter.getAmplitude() > threshold) {
                Log.d("INTERRUPT", "INTERRUPT");
                interrupt.setText("Interrupt");
                mBackgroundAudio.pause(mp);
                audioButton.setText("Resume Audio");
            } else {
                interrupt.setText("");
            }
            handler.postDelayed(this, 50);
        }
    };
    handler.postDelayed(r, 50);

While viewing this, I'm able to see the amplitude at a steady 40-50 while there is no background noise and peaks of 1200-1500 when talking quietly. I'd like the interrupt to show anytime above 800, but it's currently only showing up intermittently.

Community
  • 1
  • 1
ntgCleaner
  • 5,865
  • 9
  • 48
  • 86

1 Answers1

0

Ok, I figured it out. I tested what my amplitude was by logging the amp along with the interrupt and I saw I was getting 0. I realized I had been testing on a new amplitude (other than the amp I was showing), so I assigned the amplitude to a variable and used the variable everywhere else.

here's my outcome:

    handler = new Handler();
    final Runnable r = new Runnable() {
        @Override
        public void run() {
            mAmplitude = mSoundMeter.getAmplitude();
            mic_amplitude.setText(""+mAmplitude);
            if(mAmplitude > threshold) {
                Log.d("INTERRUPT", "INTERRUPT " + mAmplitude);
                interrupt.setText("Interrupt");
                mBackgroundAudio.pause(mp);
                audioButton.setText("Resume Audio");
            } else {
                interrupt.setText("");
            }
            handler.postDelayed(this, 100);
        }
    };
    handler.postDelayed(r, 100);
ntgCleaner
  • 5,865
  • 9
  • 48
  • 86