Using MediaRecorder and NoiseSuppressor in Android

孤人 提交于 2020-01-23 17:51:27

问题


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?


回答1:


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.




回答2:


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




回答3:


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!");
    }
}


来源:https://stackoverflow.com/questions/19527810/using-mediarecorder-and-noisesuppressor-in-android

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