How do I remove internal mic sound from the mic when recording audio in android studio?

138 Views Asked by At

I want to play a sound file and then record the audio coming out of my phone's mic right after, but the sound file keeps getting caught in the actual recording. Currently I'm having to set a 750 ms delay between the playing of the audio file and turning the mic on, but I want to be able to do them at the same time without the audio file playing over into the mic. How do I remove all noises coming from the phone's speakers?

I've tried using the AcousticEchoCanceler class, but its mostly for Java and I can't find any supported implementations for Kotlin. Plus, when I modified it for my program's use, it just caused the app to crash. Here's the code for that:

val isAvailable = AcousticEchoCanceler.isAvailable()

//debug
button.setText("$isAvailable")

if (isAvailable) {
    val aec = AcousticEchoCanceler.create(id)

    //this line causes it to crash
    aec.enabled = true
}

mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS)
mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
mediaRecorder?.setAudioSamplingRate(48000)
mediaRecorder?.setAudioEncodingBitRate(128000)

mediaRecorder?.setOutputFile(fileLoc)
mediaRecorder?.prepare()
mediaRecorder?.start()
0

There are 0 best solutions below