twilio / audioswitch

An Android audio management library for real-time communication apps.
Apache License 2.0
163 stars 50 forks source link

Audio moves to Earpiece from Speaker when incoming call ends #85

Closed BlCop closed 3 years ago

BlCop commented 3 years ago

Describe the bug During video session ( session is going on speaker) call comes on Android device. When accepting the call, video session goes into background. After while when call ends then video session comes into foreground again and at that time audio goes into Earpiece but it should be in speaker as it was in speaker before.

To Reproduce Steps to reproduce the behavior:

  1. When the video session is going on and it is on speaker, at that time call comes into the Android device.
  2. Receive the call and talk for a while.
  3. End the call
  4. Video session comes into the foreground from the background
  5. Now audio goes into Earpiece

Expected behavior Before the incoming call video session was on the speaker and after ending the call video session should be on the speaker

Actual behavior Before the incoming call video session was on the speaker and after ending the call video session goes into Earpiece.

AudioSwitch Version

Android Device (please complete the following information):

Alton09 commented 3 years ago

Hi @BlCop ! Thank you for reaching out regarding this issue. We are aware of this one and have a bug ticket to fix it in our next sprint. I'll be sure to update this ticket with the PR that resolves the issue. Thanks!

himanshu-my commented 3 years ago

Hey @Alton09 Any update on it? I am also facing this issue on some particular devices like Realme and Samsung Galaxy Note 9

Alton09 commented 3 years ago

Hi @himanshu-my . I'm currently working on this bug at the moment and will provide an update soon. Thank you for sharing those problematic devices. I'll be sure to test on them or similar devices that we have available.

Alton09 commented 3 years ago

@BlCop @himanshu-my I found a workaround that you all can use in the meantime while we implement support for this in the library. Check out this commit in the Twilio Video App. It uses PhoneStateListener to toggle AudioSwitch based on the system phone call state. More updates coming soon regarding library support.

Alton09 commented 3 years ago

@BlCop @himanshu-my After digging into this issue a bit more I have discovered that this problem can be avoided by providing a AudioManager.OnAudioFocusChangeListener implementation to the AudioSwitch constructor, which will be less boilerplate than the PhoneStateListener solution. Within the implementation you will want to call activate() every time the AUDIOFOCUS_GAIN focusChange event is received. I have noticed some odd behavior with bluetooth is active, so make sure to add a check for all audio devices besides a BluetoothHeadset. Here's what this implementation looks like:

AudioManager.OnAudioFocusChangeListener { focusChange ->
        if (focusChange == AUDIOFOCUS_GAIN && audioSwitch.selectedAudioDevice !is AudioDevice.BluetoothHeadset) { 
            audioSwitch.activate() 
        }
}

Hope this helps! Thanks.

Alton09 commented 3 years ago

Closing this one due to inactivity. Please feel free to open new issues as needed.