Using MediaRecorder and NoiseSuppressor in Android

北城以北 提交于 2019-12-07 18:06:24

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.

Raevik

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

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!");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!