Android Question Use of VOICE_COMMUNICATION (7) audioSource in Android 10+

Gandalf

Member
Licensed User
Longtime User
Hi All,

In Android 10+ it is required to enable AEC when using VOICE_COMMUNICATION (7) audioSource. I tried to do it:
B4X:
Sub Process_Globals
    Private Microphone As AudioStreamer

    Private JNoiseSuppressor As JavaObject
    Private JAcousticEchoCanceler As JavaObject
    Public NSavailable As Boolean = False
    Public AECavailable As Boolean = False
    Private MicAudioSessionId As Int = 0
End Sub

Sub Service_Create
    Microphone.Initialize2(7, "Microphone", 8000, True, 16, Microphone.VOLUME_VOICE_CALL)    'See https://developer.android.com/reference/android/media/MediaRecorder.AudioSource
    Dim r As Reflector   
    r.Target = Microphone
    Dim newBuffer(320) As Byte
    r.SetField2("recordBuffer", newBuffer)

    Dim arj As JavaObject = r.GetField("audioRecord")
    MicAudioSessionId = arj.RunMethod("getAudioSessionId", Null)
    LogColor("Mic audio session ID " & MicAudioSessionId, Colors.Green)
    
    If JAcousticEchoCanceler.IsInitialized Then JAcousticEchoCanceler.RunMethod("release", Null)
    JAcousticEchoCanceler.InitializeStatic("android.media.audiofx.AcousticEchoCanceler")
    JAcousticEchoCanceler = JAcousticEchoCanceler.RunMethod("create", Array(MicAudioSessionId))
    AECavailable = JAcousticEchoCanceler.RunMethod("isAvailable", Null)
    LogColor("AECavailable = " & AECavailable, IIf(AECavailable, Colors.Green, Colors.Red))
    If AECavailable = False Then
        LogColor("AEC is not available", Colors.Red)
    Else
        JAcousticEchoCanceler.RunMethod("setEnabled", Array(True))
        Dim isEnabled As Boolean = JAcousticEchoCanceler.RunMethod("getEnabled", Null)
        LogColor("AEC enabled: " & isEnabled, Colors.Green)
    End If
    
    If JNoiseSuppressor.IsInitialized Then JNoiseSuppressor.RunMethod("release", Null)
    JNoiseSuppressor.InitializeStatic("android.media.audiofx.NoiseSuppressor")
    JNoiseSuppressor = JNoiseSuppressor.RunMethod("create", Array(MicAudioSessionId))
    NSavailable = JNoiseSuppressor.RunMethod("isAvailable", Null)
    LogColor("NSavailable = " & NSavailable, IIf(NSavailable, Colors.Green, Colors.Red))
        If NSavailable = False Then
        LogColor("Noise Suppressor is not available", Colors.Red)
    Else
        JNoiseSuppressor.RunMethod("setEnabled", Array(True))
        Dim isEnabled As Boolean = JNoiseSuppressor.RunMethod("getEnabled", Null)
        LogColor("Noise suppressor enabled: " & isEnabled, Colors.Green)
    End If

End Sub

But it doesn't output any audio. If I change audioSource to MIC (1) or run on Android 9, audio works. Any advice how to use VOICE_COMMUNICATION source on Android 10+?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top