getMaxAmplitude() always returns 0

对着背影说爱祢 提交于 2019-12-19 21:45:46

问题


I'm trying to get the Sound Level Pressure expressed in decibel but I always get 0. (The output of the TextView is -Infinity but 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 the Manifest.xml:

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

Could you help me please?

Update

I tried with this 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

            


回答1:


Try with 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 I get values between 30 db (my room with silence) and 88 db (when I put loud music).

I think they're OK




回答2:


I think you need to call recorder.prepare() and recorder.start() as per the developer notes

UPDATE you also must setOutputFile(file_path);

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start();   // Recording is now started
 ...
recorder.stop();
recorder.reset();   // You can reuse the object by going 



回答3:


Below code worked for me, need to set "setAudioSamplingRate" and "setAudioEncodingBitRate"

 MediaRecorder recorder = new MediaRecorder();
 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 if (Build.VERSION.SDK_INT >= 10) {
  recorder.setAudioSamplingRate(44100);
  recorder.setAudioEncodingBitRate(96000);
  recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
} else {
  // older version of Android, use crappy sounding voice codec
   recorder.setAudioSamplingRate(8000);
   recorder.setAudioEncodingBitRate(12200);
   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
   recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
recorder.setOutputFile(file.getAbsolutePath());
try {
 recorder.prepare();
 } catch (IOException e) {
throw new RuntimeException(e);
}


来源:https://stackoverflow.com/questions/31305163/getmaxamplitude-always-returns-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!