1

I'm trying to detect when there is too much auditory noise. I'm new to Android, and I don't know how to do detect external noise levels.

An attempt I tried was to display the value of noise using the recorder methods: getAudioSource and getMaxAmplitude methods.

 public void startRecording() throws IOException {

                numeSunet=Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".3gp";

                recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".3gp");
   recorder.prepare();
    recorder.start();
        int x = recorder.getMaxAmplitude();

            snt.setText(""+x);

        }public void stopRecording() {
        recorder.stop();
            recorder.release();

        }

In my case, y is always 0, and x is always 8. I would appreciate if somebody knows how to detect audio noise levels in android.

1 Answers1

1

From this link:

  1. Use mRecorder.getMaxAmplitude();

  2. For the analysis of sound without saving all you need is use mRecorder.setOutputFile("/dev/null");

Here´s an example, I hope this helps

public class SoundMeter {

    private MediaRecorder mRecorder = null;

    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"); 
                    mRecorder.prepare();
                    mRecorder.start();
            }
    }

    public void stop() {
            if (mRecorder != null) {
                    mRecorder.stop();       
                    mRecorder.release();
                    mRecorder = null;
            }
    }

    public double getAmplitude() {
            if (mRecorder != null)
                    return  mRecorder.getMaxAmplitude();
            else
                    return 0;

    }
}

EDIT:

Reading into the API for MediaRecorder shows this...

public int getMaxAmplitude ()

Added in API level 1

Returns the maximum absolute amplitude that was sampled since the last call to this method. Call this only after the setAudioSource().

Returns the maximum absolute amplitude measured since the last call, or 0 when called for the first time Throws IllegalStateException if it is called before the audio source has been set.

Community
  • 1
  • 1
Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64