androidxamarinopentoktokboxvoip-android

AudioFocus request DENIED to OpenTok(Tokbox) when in same added Call, android 9 and 10


Android Telecom Manager no incoming audio/sound on an Added VOIP call

I am trying to add a VOIP video calls in my app. I have registered the phone account and added the call to TelecomManager. Call is accepted successfully. I have already implemented Connection and ConnectionService.

I am using the below code to add a call.

var uri = Android.Net.Uri.FromParts(PhoneAccount.SchemeSip, voipCallInfo.Payload?.CallerName, null);
extras.PutParcelable(TelecomManager.ExtraIncomingCallAddress, uri);
extras.PutParcelable(TelecomManager.ExtraPhoneAccountHandle, phoneAccountHandle);
telecomManager.AddNewIncomingCall(phoneAccountHandle, extras);

and I am using the below code to accept the ringing call.

var telecomManager = GetTelecomManager();
if (telecomManager == null)
{
     logger.Error("Telecom Manager is null, May be permissions not granted");
     return;
}
try
{
     .
     .
     .
     telecomManager.AcceptRingingCall();
     .
     .
}
catch (Exception ex)
{
     logger.Error("RequestSystemToAnswerCall Exception : " + ex.Message);
}

I have tried to request the audio focus, but when I add a call in the telecom manager my app loses the focus because the phone starts ringing. After I accept the call app doesn't get the focus back I believe Telecom/Call has the focus but I can't hear anything. Another person on the call can hear me without a problem. When I end the call apps get the focus back.

I can see below in the logs.

    2020-06-22,14:09:34.831 WebRTCManager Trace IsAudioSubscriptionEnabled True 
[AudioManager] Use of stream types is deprecated for operations other than volume control
[AudioManager] See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case
[AUDIO_FOCUS] Audio Focus request DENIED !

Below is the code I am using for requesting Audio.

 public bool RequestAudioFocus()
    {
        var amanager = (AudioManager)GetSystemService(AudioService);
        AudioFocusRequest audioFocusRequest;
        if (Build.VERSION.SdkInt > BuildVersionCodes.O)
        {
            audioFocusRequest = amanager.RequestAudioFocus(new AudioFocusRequestClass.Builder(AudioFocus.Gain)
                                       .SetAudioAttributes(new AudioAttributes.Builder().SetLegacyStreamType(Stream.VoiceCall).Build())
                                       .SetOnAudioFocusChangeListener(this)
                                       .Build());
        }
        else
        {
            audioFocusRequest = amanager.RequestAudioFocus(this, Stream.VoiceCall, AudioFocus.Gain);
        }

        Debug.WriteLine("MainActivity RequestAudioFocus audioFocusRequest : " + audioFocusRequest);

        if (audioFocusRequest == AudioFocusRequest.Granted)
        {

            return true;
        }
        return false;
    }

When I establish a VOIP connection without using TelecomManager. Everythings work fine. So I believe something goes wrong when I add and accept the call.

Thanks for any idea or fix.


Solution

  • The reason why you can't hear anything is because OpenTok obeys the result of the RequestAudioFocus. Since the audio focus request fails OpenTok will not play audio. You can either find out why RequestAudioFocus fails or download to Xamarin.OpenTok.Android 2.14.2 in order to play audio even if RequestAudioFocus fails.