twilio / audioswitch

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

Deactivate doesn't turn off speakerphone audio #84

Closed KrzysztofSzalach closed 3 years ago

KrzysztofSzalach commented 3 years ago

Describe the bug Calling audioSwitch.deactivate() doesn't turn off speakerphone audio output, but lowers it volume a little bit only.

To Reproduce Currently, the implementation details are very simple:

private val audioSwitch = AudioSwitch(context, false, AudioManager.OnAudioFocusChangeListener { },
    listOf(Speakerphone::class.java))
audioSwitch.start { _, _ -> }
audioSwitch.activate()

Then, when user moves app to background, we call audioSwitch.deactivate()

Expected behavior Speakerphone is muted and no audio if hearable.

Actual behavior Speakerphone has lower volume and user can still hear the voice of the video call participant.

Application Logs D/AS/AudioSwitch: AudioSwitch(1.1.1) D/AS/AudioSwitch: Preferred device list = [Speakerphone, BluetoothHeadset, WiredHeadset, Earpiece] call to AS/audioSwitch.start() D/AS/AudioDeviceManager: Speakerphone available D/AS/AudioDeviceManager: Earpiece available D/AS/AudioSwitch: Available AudioDevice list updated: [Speakerphone(name=Speakerphone), Earpiece(name=Earpiece)] D/AS/AudioSwitch: Current user selected AudioDevice = null call to audioSwitch.activate() W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state call to audioSwitch.deactivate() W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state call to audioSwitch.activate() W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state

AudioSwitch Version

Android Device (please complete the following information):

Alton09 commented 3 years ago

Hi @KrzysztofSzalach . Thanks for filing this issue. This is actually the expected behavior of the deactivate function. What it does is restores the audio focus to the state before calling the activate function, in your case the earpiece was previously active. The library currently doesn't support muting audio, but you can achieve this by calling the adjustStreamVolume method from the Android AudioManager class. Hope that helps!

KrzysztofSzalach commented 3 years ago

Hello @Alton09:

in your case the earpiece was previously active

This is not exactly true, as I didn't have any earpiece connected to my device - nor using cable output, nor with bluetooth device.

you can achieve this by calling the adjustStreamVolume

What if user turn the volume high again while app with video call is in background? I guess he will hear again voice of the other participant.

I've now double check what I can do using RemoteAudioTrack and there is a method enablePlayback(boolean) which I can use to mute audio (I didn't find this in documentation and/or Android examples of twilio video call). On the other hand, it's not very convinient to disable track on each participant if there are many, but this seems to be the only option.

Alton09 commented 3 years ago

I've now double check what I can do using RemoteAudioTrack and there is a method enablePlayback(boolean) which I can use to mute audio (I didn't find this in documentation and/or Android examples of twilio video call).

Yes, this is the only way to mute remote participant audio from the Video SDK at the moment.

I'll close this issue for now as it sounds like you have found a path forward. Please feel free to reopen if you experience additional issues. Thanks!