twilio / audioswitch

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

Apps targeting Android 14 lose input audio when phone is locked or app is in background #158

Closed AlanMTuring closed 4 months ago

AlanMTuring commented 4 months ago

Describe the bug Our production app targets Android 14 and uses the AudioSwitch. When users are on Android 14 and either lock their screen or put our app in the background, they can hear audio come through but audio input no longer works.

I've also been able to reproduce on the voice quickstart by bumping the targetSdk to 34. Doing so required the following changes to the project:

Similar to an older issue, but new for Android 14.

To Reproduce Steps to reproduce the behavior:

  1. Start a call
  2. Confirm input/output audio works
  3. Lock screen OR put app in background
  4. Input audio no longer works

Expected behavior Locking screen or putting app in background with the foreground service started should keep the input audio working

Actual behavior Locking screen or putting app in background causes the input audio to stop working

Application Logs

AudioSwitch Version

Android Device (please complete the following information):

Screenshots N/A

Additional context N/A

afalls-twilio commented 4 months ago

@AlanMTuring This is probably due to the activity being backgrounded. What happens if instead of passing the activity context to the Voice.Connect, you passed the application context?

AlanMTuring commented 4 months ago

In our production app, we are already passing in the application context to the voice connect and into the AudioSwitch creation, and the problem is still there.

With the quickstart, I edited the code in VoiceActivity.handleCallRequest() so that Voice.connect takes in the application context and in VoiceActivity.answer() so that activeCallinvite.accept() also takes in the application context. After those changes, i was still able to reproduce the issue.

afalls-twilio commented 4 months ago

@AlanMTuring ok, My next question is are you integrating with the ConnectionService API? If not, you shouldn't be using the FOREGROUND_SERVICE_PHONE_CALL and instead should use FOREGROUND_SERVICE_MICROPHONE.. For more info look here..

Continue microphone capture from the background, such as voice recorders or communication apps.

https://developer.android.com/about/versions/14/changes/fgs-types-required#microphone

AlanMTuring commented 4 months ago

Thanks, that solved it!

Just to note, we have two foreground services, an IncomingCallNotificationService and an ActiveCallNotificationService. The incoming call is still a FOREGROUND_SERVICE_PHONE_CALL and at least in our code is needed as such to show the incoming call notification. Changing the active call service to FOREGROUND_SERVICE_MICROPHONE solved the audio issue while the app is in the background and still shows the proper notification 👍