在Android中获取麦克风的声音级别

在Android中获取麦克风的声音级别

本文介绍了在Android中获取麦克风的声音级别(分贝级别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android很陌生,我已经搜索了很长时间.我想构建一个类似于分贝计的应用程序.它实时显示声音水平.房间里有很多噪音,会有声音表明,如果安静,那表明声音!.

Im quite new to android and i have searched about this for quite a while. I would like to build an application that is something like a decibel meter. In realtime it shows the sound level. It there is much noise in the room, there will be something indicating that, if its quiet something will indicate that!.

我完全不知道如何执行此操作.谁能解释一下麦克风声音级应用程序的基础知识?如果可能的话,也许提供一些代码?

I don't have any idea at all how to do this. Could anyone explain what the basics of the microphone-sound-level application? If its possible, maybe provide some code?

谢谢!

推荐答案

您可以使用 MediaRecorder.getMaxAmplitude().

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
            RECORD_AUDIO);
}
  1. 使用MediaRecorder获取噪声水平,

  1. Get the noise level using the MediaRecorder,

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null");
try {
    mRecorder.prepare();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
mRecorder.start();

  • 启动MediaRecorder,

  • Start the MediaRecorder,

    private Runnable mSleepTask = new Runnable() {
        public void run() {
            //Log.i("Noise", "runnable mSleepTask");
            mSensor.start();
            if (!mWakeLock.isHeld()) {
                 mWakeLock.acquire();
            }
            //Noise monitoring start
            // Runnable(mPollTask) will execute after POLL_INTERVAL
            mHandler.postDelayed(mPollTask, POLL_INTERVAL);
        }
    };
    

  • 创建可运行线程以经常检查噪声水平,

  • Create Runnable Thread to check the noise level frequently,

    private Runnable mPollTask = new Runnable() {
        public void run() {
            double amp = mSensor.getAmplitude();
            //Log.i("Noise", "runnable mPollTask");
            // Runnable(mPollTask) will again execute after POLL_INTERVAL
            mHandler.postDelayed(mPollTask, POLL_INTERVAL);
        }
    };
    

  • 使用以下公式将幅度转换为分贝

    Convert the Amplitude to decibel using the following formula,

    return 20 * Math.log10(mRecorder.getMaxAmplitude() / 2700.0);
    
    1. 监视声音并发出大声警报.

    1. Monitoring the Voice and Alert for the Louder Noise.

    // Create runnable thread to Monitor Voice
    private Runnable mPollTask = new Runnable() {
        public void run() {
            double amp = mSensor.getAmplitude();
            //Log.i("Noise", "runnable mPollTask");
            updateDisplay("Monitoring Voice...", amp);
    
            if ((amp > mThreshold)) {
                callForHelp(amp);
                //Log.i("Noise", "==== onCreate ===");
            }
            // Runnable(mPollTask) will again execute after POLL_INTERVAL
            mHandler.postDelayed(mPollTask, POLL_INTERVAL);
        }
    };
    

    这篇关于在Android中获取麦克风的声音级别(分贝级别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-04 08:00