twilio / audioswitch

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

IllegalStateException com.twilio.audioswitch.AudioSwitch in activate #113

Closed velevtzvetlin closed 2 years ago

velevtzvetlin commented 2 years ago

Seems like the exception is thrown in this method

fun activate() {
        when (state) {
            STARTED -> {
                audioDeviceManager.cacheAudioState()

                // Always set mute to false for WebRTC
                audioDeviceManager.mute(false)
                audioDeviceManager.setAudioFocus()
                selectedDevice?.let { activate(it) }
                state = ACTIVATED
            }
            ACTIVATED -> selectedDevice?.let { activate(it) }
            STOPPED -> throw IllegalStateException()
        }
    }

That being said, I am calling the start method and ive verified i haven't called stop. Which would be the only method that puts the state into STOPPED. This isn't a consistent issue, in fact when im running my app locally i cannot reproduce it but my production app is throwing this error randomly on Sentry.

afalls-twilio commented 2 years ago

@velevtzvetlin Thank you for filing a report. Can you see if the start is failing, thus never switching to the STARTED state? I suspect what is happening is that the required permissions are not being accepted by the user, then its failing to start.

velevtzvetlin commented 2 years ago

@afalls-twilio thanks for the response, so im using this in a react-native package. Here is where i call start (when the app starts up and the module is instantiated with the app's context: https://github.com/NowRx/react-native-twilio-voice-sdk/blob/3cc6d8e9d9f84139a5cc61b1d3e4761239be1dc8/android/src/main/java/com/happytheapp/react/RNTwilioVoiceSDK/TwilioVoiceSDKModule.java#L83

This function attempts to create the call using twilio voice, starts a ringer loop, and calls manipulateAudioManagerBasedOnBLE here: https://github.com/NowRx/react-native-twilio-voice-sdk/blob/3cc6d8e9d9f84139a5cc61b1d3e4761239be1dc8/android/src/main/java/com/happytheapp/react/RNTwilioVoiceSDK/TwilioVoiceSDKModule.java#L320

which selects a audio device and then calls activate. then on lines 173, 329 i am calling audioswitch.deactivate(). I understand your point but im not very experienced in java. Is calling start a synchronous task meaning once that call runs I can call activate right after it?