繁体   English   中英

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

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

我正在尝试检测何时有过多的听觉噪声。 我是Android的新手,我不知道如何检测外部噪声水平。

我尝试过使用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();

        }

在我的情况下,y始终为0,x始终为8。如果有人知道如何检测android中的音频噪声水平,我将不胜感激。

从此链接

  1. 使用mRecorder.getMaxAmplitude();

  2. 要进行声音分析而不保存所有内容,请使用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;

    }
}

编辑:

阅读MediaRecorderAPI会显示此信息...

public int getMaxAmplitude()

在API级别1中添加

返回自上次调用此方法以来采样的最大绝对振幅。 仅在setAudioSource()之后调用此函数。

返回自上次调用以来测得的最大绝对振幅,如果是第一次调用,则返回0。如果在设置音频源之前调用,则抛出IllegalStateException。

暂无
暂无

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

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