Skip to end of metadata
Go to start of metadata

It is necessary to get sound level value to test microphone before streaming. The standard class android.media.MediaRecorder can be used to do that. In this case, audio data should be recorded to a file. Android 10 and lower allows recording to the pseudodevice /dev/null. but in Android 11 and newer this is impossible, and a real file stored in application data must be used:

code

            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            String fileName = context.getFilesDir().getAbsolutePath() + "/test.3gp";
            //Path /dev/null throw exception on Android 11
            mRecorder.setOutputFile(fileName);

            try {
                mRecorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mRecorder.start();

An instantaneous value of sound amplitude may be received from MediaRecorder object

code

    public double getAmplitude() {
        if (mRecorder != null)
            return (mRecorder.getMaxAmplitude() / 2700.0);
        else
            return 0;

    }

and then average this value to display it

code

    static final private double EMA_FILTER = 0.6;
    private double mEMA = 0.0;
    ...
    public double getAmplitudeEMA() {
        double amp = getAmplitude();
        mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
        return mEMA;
    }
  • No labels