android - getMaxAmplitude() always returns 0 -


i'm trying sound level pressure expressed in decibel 0. (the output of textview -infinity because log(0) = -infinity.

public class slp extends activity{  textview sound;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.sound_activity);      mediarecorder recorder = new mediarecorder();     recorder.setaudiosource(mediarecorder.audiosource.mic);     recorder.setoutputformat(mediarecorder.outputformat.three_gpp);     recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb);     timer timer = new timer();     timer.scheduleatfixedrate(new recordertask(recorder), 0, 500);   }  private class recordertask extends timertask {     textview sound = (textview) findviewbyid(r.id.decibel);     private mediarecorder recorder;      public recordertask(mediarecorder recorder) {         this.recorder = recorder;     }      public void run() {         runonuithread(new runnable() {             @override             public void run() {                 int amplitude = recorder.getmaxamplitude();                 double amplitudedb = 20 * math.log10((double)math.abs(amplitude) / 32768);                 sound.settext("" + amplitudedb);             }         });     } } 

the permission in manifest.xml:

<uses-permission android:name="android.permission.record_audio" /> 

could me please?

update

i tried too:

mediarecorder recorder = new mediarecorder();     recorder.setaudiosource(mediarecorder.audiosource.mic);     recorder.setoutputformat(mediarecorder.outputformat.three_gpp);     recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb);     recorder.setoutputfile("/dev/null");     try {         recorder.prepare();     } catch (ioexception e) {         e.printstacktrace();     }     recorder.start();     timer timer = new timer();     timer.scheduleatfixedrate(new recordertask(recorder), 0, 500);     recorder.reset(); } private class recordertask extends timertask {     textview risultato = (textview) findviewbyid(r.id.decibel);     private mediarecorder recorder;      public recordertask(mediarecorder recorder) {         this.recorder = recorder;     }      public double getamplitude() {         if (recorder != null)             return  (recorder.getmaxamplitude());         else             return 0;      }      public void run() {         runonuithread(new runnable() {             @override             public void run() {                 int amplitude = recorder.getmaxamplitude();                 double amplitudedb = 20 * math.log10(getamplitude() / 32768);                 //double db = 20 * math.log(recorder.getmaxamplitude() / 2700.0);                 risultato.settext("" + amplitudedb);             }         });     } } 

but nothing, still getting -infinite

            

try this:

mediarecorder recorder = new mediarecorder();     recorder.setaudiosource(mediarecorder.audiosource.mic);     recorder.setoutputformat(mediarecorder.outputformat.three_gpp);     recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb);     timer timer = new timer();     timer.scheduleatfixedrate(new recordertask(recorder), 0, 500);     recorder.setoutputfile("/dev/null");      try {         recorder.prepare();         recorder.start();     } catch(illegalstateexception e)     {         e.printstacktrace();     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     }   }  private class recordertask extends timertask {     textview sound = (textview) findviewbyid(r.id.decibel);     private mediarecorder recorder;      public recordertask(mediarecorder recorder) {         this.recorder = recorder;     }      public void run() {         runonuithread(new runnable() {             @override             public void run() {                 int amplitude = recorder.getmaxamplitude();                 double amplitudedb = 20 * math.log10((double)math.abs(amplitude));                  sound.settext("" + amplitudedb);             }         });     } } 

without / 32768 values between 30 db (my room silence) , 88 db (when put loud music).

enter image description here

i think they're ok


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -