开发者

Get microphone soundlevel in android

Im trying to get the sound level by getting live-input from the microphone.

As apps such as ,Sound meter and deciBel. I found this sample code from the link:

http://code.google.com/p/android-labs/source/browse/trunk/NoiseAlert/src/com/google/android/noisealert/SoundMeter.java

I'm also pasting it here.

package com.google.android.noisealert;


import android.media.MediaRecorder;

public class SoundMeter {
    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"); 
                mRecorder.prepare();
                mRecorder.start();
                mEMA = 0.0;
            }
    }

    public void stop() {
            if (mRecorder != null) {
       开发者_开发技巧             mRecorder.stop();       
                    mRecorder.release();
                    mRecorder = null;
            }
    }

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

    }

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

}

Does this code do what im trying to do? Thanks!


it will once you:

  • instantiate the class
  • call its start() method
  • poll its getAmplitude() function from your main class (just as they did it in that sample code). Be aware that you need to do the polling in a Runnable() so that the UI is not affected. (Call the getAmplitude function every 100ms to detect a change in the input volume)

I have used it for that too and the code does the job.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜