java 在 Android 中获取麦克风声级(分贝级)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7197798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Microphone sound level ( Decibel level) in Android
提问by Seb
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!.
我对android很陌生,我已经搜索了很长一段时间。我想构建一个类似于分贝计的应用程序。它实时显示声级。如果房间里有很多噪音,就会有什么迹象表明,如果它安静,就会有什么迹象表明!。
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?
我根本不知道如何做到这一点。谁能解释一下麦克风声级应用程序的基础知识?如果可能的话,也许提供一些代码?
Thanks!
谢谢!
回答by Tung Duong
You can use MediaRecorder.getMaxAmplitude()
.
您可以使用MediaRecorder.getMaxAmplitude()
.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
RECORD_AUDIO);
}
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();
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); } };
使用 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();
启动媒体记录器,
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); } };
创建 Runnable Thread 以经常检查噪音水平,
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);
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); } };
监控声音和警报是否有更大的噪音。
// 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); } };
回答by timemirror
There's a great app in the marketplace called Audalyzer
市场上有一个很棒的应用程序叫做 Audalyzer
http://code.google.com/p/moonblink/wiki/Audalyzer
http://code.google.com/p/moonblink/wiki/Audalyzer
Also check this discussion..
也检查这个讨论..
Android: sample microphone without recording to get live amplitude/level?
回答by Dominic Cerisano
This question has been addressed generally for Java, and the required classes are available in Android.
这个问题已在 Java 中得到普遍解决,所需的类在 Android 中可用。
The basic idea is to sample the data line for the microphone, and calculate the level from the returned buffer.
基本思想是对麦克风的数据线进行采样,并从返回的缓冲区中计算电平。
How to calculate the level/amplitude/db of audio signal in java?
You can also have a look at the Visualizer class which does FFT frequency analysis, however the permissions for microphone may not be consistent across various devices. You may also have to connect it to the Equalizer class to access the mic.
您还可以查看进行 FFT 频率分析的 Visualizer 类,但是麦克风的权限在各种设备上可能不一致。您可能还必须将其连接到均衡器类才能访问麦克风。
https://developer.android.com/reference/android/media/audiofx/Visualizer.html
https://developer.android.com/reference/android/media/audiofx/Visualizer.html