getMaxAmplitude()始终返回0

时间:2015-07-08 22:47:55

标签: android decibel

我正试图在Sound Level Pressure中表达decibel,但我总是0。 (TextView的输出为 -Infinity ,但因为 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);
            }
        });
    }
}

但没有,仍然 -Infinite

3 个答案:

答案 0 :(得分:5)

试试这个:

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分贝(我的房间有静音)和88分贝(当我放大声音乐时)之间的值。

enter image description here

我认为他们没问题

答案 1 :(得分:1)

我认为您需要根据开发者说明调用recorder.prepare()和recorder.start()

更新你还必须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 

答案 2 :(得分:0)

下面的代码对我有用,需要设置&#34; setAudioSamplingRate&#34;和&#34; setAudioEncodingBitRate&#34;

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