Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I had build an application using following source code (following the steps). package com.decibal.level; import androidx.appcompat.app.AppCompatActivity; import android.media.MediaRecorder; ...
#1: Initial revision
How to manage decibel meter?
I had build an application using following source code (following [the steps](https://stackoverflow.com/a/11019896/13146129)). ``` package com.decibal.level; import androidx.appcompat.app.AppCompatActivity; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView mStatusView; MediaRecorder mediaRecorder; Thread runner; private static double mEMA = 0.0; static final private double EMA_FILTER = 0.6; final Runnable updater = new Runnable() { @Override public void run() { updateTv(); }; }; final Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mStatusView = findViewById(R.id.statusView); if (runner == null){ runner = new Thread(){ public void run(){ while(runner!=null){ try{ Thread.sleep(1000); Log.d("Noise","Tock"); }catch (InterruptedException e){ e.printStackTrace(); } handler.post(updater); } } }; runner.start(); Log.d("Noise","start runner()"); } } public void onResume() { super.onResume(); startRecorder(); } public void onPause() { super.onPause(); stopRecorder(); } public void startRecorder(){ if (mediaRecorder == null){ mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile("/dev/null"); try{ mediaRecorder.prepare(); }catch (java.io.IOException ioe){ android.util.Log.e("[Monkey]", "IOException: " + android.util.Log.getStackTraceString(ioe)); }catch (java.lang.SecurityException e){ android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e)); } try{ mediaRecorder.start(); }catch (java.lang.SecurityException e){ android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e)); } //mEMA = 0.0; } } public void stopRecorder(){ if (mediaRecorder!=null){ mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; } } public void updateTv(){ mStatusView.setText(Double.toString((getAmplitudeEMA()))+"dB"); } public double soundDb(double ampl){ return 20 * Math.log10(getAmplitudeEMA()/ampl); } public double getAmplitudeEMA() { double amp = getAmplitude(); mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA; return mEMA; } public double getAmplitude() { if (mediaRecorder != null) return (mediaRecorder.getMaxAmplitude()); else return 0; } } ``` I am listing decibel meter result below : >59.28dB<br/> 72.12480000000001dB<br/> 81.13996800000001dB<br/> 71.78239488000001dB<br/> 66.2051831808dB<br/> 73.232829308928dB<br/> 64.03725268942848dB<br/> 66.16596043030856dB<br/> 66.50655366884938dB<br/> When I speak >101.3610485870159dB <br/> 354.1377677739226dB<br/> When i don't speak anymore > 56.662042843827614dB<br/> 9.06592685501242dB<br/> 1.4505482968019872dB<br/> When I was trying to check decibel meter using [the sound meter](https://chrome.google.com/webstore/detail/sound-meter-noise-decibel/fhamlklnpkhdfepaipljcngncafnlbfa/related). The meter was at 60-71(When I speak also). The source code is correct but, the equation is wrong(that's what I think). When I don't speak than, I just can hear fan rotation sound (noise) which is not too much. Phone can detect the sound also. But, I worry when I stop speaking than decibel meter drops. How can I manage the decibel meter?