用android测量音量

时间:2014-09-04 15:33:40

标签: java android signal-processing audio-recording

我想测量外面噪音的价值。是否有一种简单的方法(示例函数有助于)找到它?我在互联网上搜索但是大多数文章都是旧的并且告诉说没有正式的方法做到这一点。

1 个答案:

答案 0 :(得分:2)

您可以在录制开始时启动另一个线程,并使用getMaxAmplitude函数捕获振幅。 下面是我们每250毫秒采样一次并计算出最大振幅的代码。

public void run() {
        int i = 0;
        while(i == 0) {

            try {
                sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (mRecorder != null) {
                amplitude = mRecorder.getMaxAmplitude();

                //Here you can put condition (low/high)
                Log.i("AMPLITUDE", new Integer(amplitude).toString());
            } 

        }
    }


OR


提供此link

AudioRecord recorder; // our recorder, must be initialized first
short[] buffer; // buffer where we will put captured samples
DataOutputStream output; // output stream to target file
boolean isRecording; // indicates if sound is currently being captured
ProgressBar pb; // our progress bar 
while (isRecording) {
    double sum = 0;
    int readSize = recorder.read(buffer, 0, buffer.length);
    for (int i = 0; i < readSize; i++) {
        output.writeShort(buffer [i]);
        sum += buffer [i] * buffer [i];
    }
    if (readSize > 0) {
        final double amplitude = sum / readSize;
        pb.setProgress((int) Math.sqrt(amplitude));
    }
}