java - 如何使用 Recorder 检测 android 中的环境噪声

标签 java android mediarecorder

我正在尝试检测何时有过多的听觉噪音。我是 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 中的音频噪声级别,我将不胜感激。

最佳答案

从这里link :

  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;

    }
}

编辑:

读入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/

相关文章:

android - 如何在android中的webview上加载网页?

android - 在 android 中更改 MediaRecorder 源

android - 如何在 Android 框架中打印日志消息

java - 使用java编程语言合并一些ogg音频文件

java - hibernate - 1 :N - The parent is duplicated upon saving the child

java - 如何调用具有多个数组作为输入的方法?

java - 在 java.time 中从 `Instant` 获取 `ZonedDateTime`

安卓 : ScrollView + ViewPager doesn't work properly

java - 尝试在 Java 中更新 MySQL 数据库时数组索引越界异常

android - 谷歌地图需要很长时间才能加载