我正在尝试检测何时有过多的听觉噪音。我是 Android 新手,我不知道如何检测外部噪音水平。
我曾尝试使用 recorder
方法显示噪声值:getAudioSource
和 getMaxAmplitude
方法。
public void startRecording() throws IOException {
numeSunet=Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".3gp";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".3gp");
recorder.prepare();
recorder.start();
int x = recorder.getMaxAmplitude();
snt.setText(""+x);
}public void stopRecording() {
recorder.stop();
recorder.release();
}
在我的例子中,y 始终为 0,x 始终为 8。 如果有人知道如何检测 android 中的音频噪声级别,我将不胜感激。
最佳答案
从这里link :
使用 mRecorder.getMaxAmplitude();
要在不保存的情况下分析声音,您只需要使用 mRecorder.setOutputFile("/dev/null");
举个例子,希望对你有帮助
public class SoundMeter {
private MediaRecorder mRecorder = null;
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();
}
}
public void stop() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
public double getAmplitude() {
if (mRecorder != null)
return mRecorder.getMaxAmplitude();
else
return 0;
}
}
编辑:
读入API for MediaRecorder显示这个...
public int getMaxAmplitude ()
在 API 级别 1 中添加
Returns the maximum absolute amplitude that was sampled since the last call to this method. Call this only after the setAudioSource().
Returns the maximum absolute amplitude measured since the last call, or 0 when called for the first time Throws IllegalStateException if it is called before the audio source has been set.
关于java - 如何使用 Recorder 检测 android 中的环境噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922134/