Raevik
Raevik

Reputation: 1991

Using MediaRecorder and NoiseSuppressor in Android

I'm starting off a project experimenting with the Android microphone using code like this:

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

After that, a prepare() and start() to begin recording mic audio.

Trouble is, I'm trying to also add in some audio processing effects like NoiseSuppressor. The API docs state that NoiseSuppressor is done with this:

 NoiseSuppressor create (int audioSession)

What is the appropriate method for initializing the recording stream and getting the audioSession for that stream? I'm surprised to find that I cannot get the audioSession from the mediaRecorder.

Why do two approaches to setting up the audio stream exist? I see the AudioRecord approach, but then the API docs suggest the above approach is preferred.

What gives?

Upvotes: 3

Views: 8427

Answers (3)

Hitesh Sahu
Hitesh Sahu

Reputation: 45150

From Android Developers:

To attach the NoiseSuppressor to a particular AudioRecord, specify the audio session ID of this AudioRecord when creating the NoiseSuppressor. The audio session is retrieved by calling AudioRecord.getAudioSessionId() on the AudioRecord instance.

Which means NoiseSuppressor requires audioSessionId to create noise suppressor instance like this

val suppressor = NoiseSuppressor.create(
            recorder!!.audioSessionId)

if you look at getaudiosessionid reference then you will see that audio session can only be created by Media Player or Audio Recorder.

Hence you can't use Noise Suppressor along with Media Recorder. However Noise Suppressor can be inserted by default in the capture path by the platform developers according to the MediaRecorder.AudioSource used.

Anyways if you still want to give Audio Recorder a try then I would say I already tried to enable NS in Audio Recorder & when I called NoiseSuppressor.isAvailable() it always returned false.

Upvotes: 5

Sam
Sam

Reputation: 75

If you want to NoiseSuppressor then you have to use AudioManager along with MediaRecorder. For Noise Suppression please use below code :

// The following would take effect only on Jelly Bean and higher.

 audioManager.setMode(AudioManager.MODE_IN_CALL);
    audioManager.setParameters("noise_suppression=on");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    Log.i("Trying to clean up audio because running on SDK " + Build.VERSION.SDK_INT);

    if (noise && NoiseSuppressor.create(getAudioSessionId()) == null) {
        Log.i("NoiseSuppressor not present :(");
    } else {
        Log.i("NoiseSuppressor enabled!");
    }

    if (gain && AutomaticGainControl.create(getAudioSessionId()) == null) {
        Log.i("AutomaticGainControl not present :(");
    } else {
        Log.i("AutomaticGainControl enabled!");
    }

    if (echo && AcousticEchoCanceler.create(getAudioSessionId()) == null) {
        Log.i("AcousticEchoCanceler not present :(");
    } else {
        Log.i("AcousticEchoCanceler enabled!");
    }
}

Upvotes: 0

Raevik
Raevik

Reputation: 1991

I found the answer to be that MediaRecorder simply isn't suited for my type of processing. I was looking to capture mic audio and do playback in real-time. The better solution is to use AudioTrack and AudioRecord.

This topic discusses it nicely:

Android: Need to record mic input

Upvotes: 0

Related Questions