getMaxAmplitude() returns wrong values in different cases

83 views Asked by At

App is running, MediaRecoder object is created and getMaxAmplitude() called every second. It returns value range 300-1000. It's seems to be normal. BUT! After incoming call is received or I call to somebody and after ending call I see that getMaxAmplitude() returns 8-10 value. Why it happens?

I always recreate MediaRecorder object before/after calling, create before start recording and destroy MediaRecorder object after stop recording. May be problem in it?

On Genymotion emulator it doesn't happen only on a real device with Android 4.2.2

public class Recorder {

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

}

For example, In service I do this: Recorder recorder = new Recorder(); and then calling if(!recorder.startRecording()) {... stopSelf(); ...} and in onDestroy() something like this: recorder.stopRecording();

0

There are 0 answers