本文介绍了getMaxAmplitude()总是返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让声级pressure 前$ P $在 pssed分贝,但我总是 0 。 (输出端的的TextView -Infinity 但由于的日志(0)= -Infinity

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);
            }
        });
    }
}

在权限的Manifest.xml

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

你能帮助我吗?

更新

我想这个也:

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);
            }
        });
    }
}

但什么都没有,仍然得到无穷大

推荐答案

试试这个:

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);
            }
        });
    }
}

如果没有的 / 32768 的我拿到30 DB(我的房间沉默)和88个DB(当我把吵闹的音乐)。之间的值

Without / 32768 I get values between 30 db (my room with silence) and 88 db (when I put loud music).

我觉得他们确定

这篇关于getMaxAmplitude()总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:43