getMaxAmplitude()在不同情况下返回错误的值

时间:2016-12-27 10:03:40

标签: android android-mediarecorder

应用正在运行,MediaRecoder对象已创建,getMaxAmplitude()每秒都会调用一次。它返回值范围300-1000。这似乎很正常。但!收到来电或我打电话给某人后,在结束通话后,我看到getMaxAmplitude()返回8-10值。为什么会这样?

我总是在调用之前/之后重新创建MediaRecorder对象,在开始记录之前创建并在停止记录后销毁MediaRecorder对象。可能有问题吗?

在Genymotion模拟器上,它不会仅在具有Android 4.2.2的真实设备上发生

公共类记录器{

private MediaRecorder mRecorder = null;
private boolean isRecording = false;

public Recorder() {}

private boolean initRecorder()
{
    try {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mRecorder.setOutputFile("/dev/null");
        return true;
    }
    catch (RuntimeException e)
    {
        return false;
    }
}

public boolean isRecording()
{
    return isRecording;
}

private boolean prepareAndStartRecord()
{
    try {
        mRecorder.prepare();
    } catch (IOException e) {
        return false;
    } catch (IllegalStateException e) {
        return false;
    }

    try {
        mRecorder.start();
        isRecording = true;
        return true;
    } catch (IllegalStateException e) {
        return false;
    }
}

public boolean startRecording()
{
    if (mRecorder == null)
    {
        if(initRecorder())
        {
            return prepareAndStartRecord();
        }
        return false;
    }
    else
    {
        if(prepareAndStartRecord())
            return true;
    }
    return false;
}

public boolean stopRecording()
{
    if (mRecorder != null)
    {
        try {
            mRecorder.stop();
            isRecording = false;
        } catch(RuntimeException e) {
            return false;
        } finally {
            mRecorder.reset();
            mRecorder.release();
            mRecorder = null;
        }
        return true;
    }
    return true;
}

public int getSPL()
{
    int spl = 0;
    if(mRecorder != null) {
        int amp = mRecorder.getMaxAmplitude();
        System.out.println("mRecorder.getMaxAmplitude = "+amp);
        spl = ((Double) Math.ceil(20 * Math.log10((amp / 51805.5336f) / 0.00002f))).intValue();
        //spl = ((Double) Math.ceil(20 * Math.log10((double)Math.abs(amp)))).intValue();
    }
    return spl > 0 ? spl : 0;
}

}

例如,在服务中我执行此操作:Recorder recorder = new Recorder();然后调用if(!recorder.startRecording()) {... stopSelf(); ...}并在onDestroy()中执行以下操作:recorder.stopRecording();

0 个答案:

没有答案