2014-09-04 77 views
0

我想测量外部噪音的值。是否有一种简单的方法(示例函数可以帮助我)找到答案?我在互联网上搜索,但大多数文章都是旧的,并告诉他们没有官方的方式来做到这一点。使用android测量音量

回答

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


Reffer这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)); 
    } 
}