0
我想測量外部噪音的值。是否有一種簡單的方法(示例函數可以幫助我)找到答案?我在互聯網上搜索,但大多數文章都是舊的,並告訴他們沒有官方的方式來做到這一點。使用android測量音量
我想測量外部噪音的值。是否有一種簡單的方法(示例函數可以幫助我)找到答案?我在互聯網上搜索,但大多數文章都是舊的,並告訴他們沒有官方的方式來做到這一點。使用android測量音量
錄製開始時可以啓動另一個線程,並使用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));
}
}