twilio / audioswitch

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

Audio routing from BT to Speaker #163

Open tech-ajit-mob opened 1 month ago

tech-ajit-mob commented 1 month ago

Describe the bug Audio is breaking or sometimes coming via Bluetooth and Speaker/Earpiece after changing from Bluetooth to speaker/Earpiece mode.

To Reproduce Steps to reproduce the behavior: Case 1:

  1. Pre-condition Bluetooth connected and voice is coming via Bluetooth properly.
  2. Switch Audio from Bluetooth to Speaker mode.
  3. Voice is breaking or sometimes coming via Bluetooth and Speaker.

Case 2:

  1. Pre-condition Bluetooth connected and voice is coming via Bluetooth properly.
  2. Switch Audio from Bluetooth to Earpiece mode.
  3. Voice coming via Bluetooth and Earpiece like 1 sec in Bluetooth and next second in Earpiece i.e. continuous process.

Case 3: Working fine

  1. Bluetooth is not connected.
  2. Audio is coming via Earpiece and working fine.
  3. Change audio mode from Earpiece to Speaker, working fine.
  4. Change audio mode from Speaker to Earpiece , working fine.

Expected behavior Voice is coming via selected audio mode.

Actual behavior The voice coming via Bluetooth and Earpiece like 1 sec in Bluetooth and the next 1 second in Earpiece again 1 sec in BT and next 1 sec in Earpiece etc.

Application Logs

https://github.com/twilio/audioswitch/assets/134035235/ada0064b-72d7-4e83-805c-bea81475d77b

AudioSwitch Version

Android Device (please complete the following information):

Screenshots If applicable, add screenshots to help explain your problem.

Additional context I have attached a log file along with recorded audio in which you can listen to audio coming via speaker and BT after switching from BT to speaker.

The code is given below:

private fun displayCallOption(availableAudioDevices: List<AudioDevice>, audioSwitch: AudioSwitch) {
        preparedConnectedResources(availableAudioDevices)
        voiceCallViewModel.displayConnectedResourcesBottomSheet(this, connectedResources.toList(), voiceCallInfo, object : OnConnectedBluetoothBottomItemClickListener {
            override fun onItemClick(connectedBluetoothResource: ConnectedBluetoothResource, index: Int) {
                val selectedAudioDevice = availableAudioDevices[index]
                updateAudioDeviceIcon(selectedAudioDevice)
                audioSwitch.selectDevice(selectedAudioDevice)
            }
        })
    }
tech-ajit-mob commented 1 month ago

@afalls-twilio did you get a chance to look up this issue? It's 100% reproducible.

tech-ajit-mob commented 1 month ago

13:02:38.024 D Current user selected AudioDevice = null 13:02:38.033 D Current user selected AudioDevice = null 13:02:38.037 D Current user selected AudioDevice = null 13:02:38.041 D Current user selected AudioDevice = null 13:02:38.048 D Current user selected AudioDevice = null 13:02:38.052 D Current user selected AudioDevice = null 13:02:38.056 D Current user selected AudioDevice = null 13:02:38.061 D Current user selected AudioDevice = Speakerphone(name=Speakerphone) 13:02:38.067 D Current user selected AudioDevice = null 13:02:38.071 D Current user selected AudioDevice = null 13:02:38.074 D Current user selected AudioDevice = Earpiece(name=Earpiece) 13:02:38.079 D Current user selected AudioDevice = Speakerphone(name=Speakerphone) 13:02:38.281 D Current user selected AudioDevice = null 13:02:38.287 D Current user selected AudioDevice = null

See the log, the selected audio device is the Speaker.

afalls-twilio commented 1 month ago

@tech-ajit-mob thank you for the info, we will circle back with you when get a chance to investigate.

tech-ajit-mob commented 1 month ago

Thank you so much @afalls-twilio for paying attention to this genuine and serious issue. If you want any supporting documents, logs, or recordings I am always available, please figure out and fix it as soon as possible. Based on Twilio's recommendation we are using this library.

tech-ajit-mob commented 1 month ago

More details:

  override fun onDisconnected(call: Call, callException: CallException?) {
            _audioSwitch.deactivate()
}

 override fun onConnected(call: Call) {
            _audioSwitch.activate()
        }

Note: I am not calling _audioSwitch.stop() after disconnecting the call

ocarevs commented 4 weeks ago

@tech-ajit-mob It appears that your bluetooth headset is connected to multiple devices at the same time, i.e. two or more android devices. Make sure that the bluetooth device is connected to one android device at a time. Let us know if that resolves the issue.

tech-ajit-mob commented 4 weeks ago

Thanks for the update @ocarevs I will let you know.

tech-ajit-mob commented 1 week ago

Hello @ocarevs , The issue has been fixed with the single connected device but kept under observation for multiple connected devices.

Changes I did

  1. After getting Bluetooth permission response stop the audio switch

mService?.getAudioSwitch()?.stop()

  1. Start the audio switch again

startAudioSwitch()

private fun startAudioSwitch() {
        /*
         * Start the audio device selector after the menu is created and update the icon when the
         * selected audio device changes.
         */
        mService?.let {
            it.getAudioSwitch().start { _: List<AudioDevice?>?, selectedDevice: AudioDevice? ->
                updateAudioDeviceIcon(selectedDevice)
            }
        }
    }