我想测量外部噪声的值,是否有一种简便的方法(示例函数可以帮助发现)?我在互联网上进行搜索,但大多数文章都是陈旧的,并告诉我们没有官方的方法可以做到这一点。
最佳答案
您可以在记录开始时启动另一个线程,并使用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());
}
}
}
要么
引用此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));
}
}
关于java - 用Android测量音量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25669489/