简体   繁体   English

如何使用Recorder在android中检测环境噪声

[英]How to detect ambient noise in android using Recorder

I'm trying to detect when there is too much auditory noise. 我正在尝试检测何时有过多的听觉噪声。 I'm new to Android, and I don't know how to do detect external noise levels. 我是Android的新手,我不知道如何检测外部噪声水平。

An attempt I tried was to display the value of noise using the recorder methods: getAudioSource and getMaxAmplitude methods. 我尝试过使用recorder方法( getAudioSourcegetMaxAmplitude方法)显示噪声值。

 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();

        }

In my case, y is always 0, and x is always 8. I would appreciate if somebody knows how to detect audio noise levels in android. 在我的情况下,y始终为0,x始终为8。如果有人知道如何检测android中的音频噪声水平,我将不胜感激。

From this link : 从此链接

  1. Use mRecorder.getMaxAmplitude(); 使用mRecorder.getMaxAmplitude();

  2. For the analysis of sound without saving all you need is use mRecorder.setOutputFile("/dev/null"); 要进行声音分析而不保存所有内容,请使用mRecorder.setOutputFile(“ / dev / null”);。

Here´s an example, I hope this helps 这是一个例子,希望对您有所帮助

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;

    }
}

EDIT: 编辑:

Reading into the API for MediaRecorder shows this... 阅读MediaRecorderAPI会显示此信息...

public int getMaxAmplitude () public int getMaxAmplitude()

Added in API level 1 在API级别1中添加

Returns the maximum absolute amplitude that was sampled since the last call to this method. 返回自上次调用此方法以来采样的最大绝对振幅。 Call this only after the setAudioSource(). 仅在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. 返回自上次调用以来测得的最大绝对振幅,如果是第一次调用,则返回0。如果在设置音频源之前调用,则抛出IllegalStateException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM