An Android audio management library for real-time communication apps.
- Manage audio focus for typical VoIP and Video conferencing use cases.
- Manage audio input and output devices.
- Detect changes in available audio devices
- Enumerate audio devices
- Select an audio device
| Android Studio Version | Android API Version Min |
|---|---|
| 3.6+ | 16 |
To get started using this library, follow the steps below.
Add this line as a new Gradle dependency:
implementation 'com.twilio:audioswitch:$version'Instantiate an instance of the AudioDeviceSelector class, passing a reference to the application context.
val audioDeviceSelector = AudioDeviceSelector(applicationContext)To begin listening for live audio device changes, call the start function and pass a lambda that will receive AudioDevices when they become available.
audioDeviceSelector.start { audioDevices, selectedDevice ->
// TODO update UI with audio devices
}You can also retrieve the available and selected audio devices manually at any time by calling the following properties:
val devices: List<AudioDevice> = audioDeviceSelector.availableAudioDevices
val selectedDevice: AudioDevice? = audioDeviceSelector.selectedAudioDeviceNote: Don't forget to stop listening for audio devices when no longer needed in order to prevent a memory leak.
audioDeviceSelector.stop()Before activating an AudioDevice, it needs to be selected first.
devices.find { it is AudioDevice.Speakerphone }?.let { audioDeviceSelector.selectDevice(it) }If no device is selected, then the library will automatically select a device based on the following priority: BluetoothHeadset -> WiredHeadset -> Earpiece -> Speakerphone.
Activating a device acquires audio focus with voice communication usage and begins routing audio input/output to the selected device.
audioDeviceSelector.activate()Make sure to revert back to the prior audio state when it makes sense to do so in your app.
audioDeviceSelector.deactivate()Note: The stop() function will call deactivate() before closing AudioDeviceSelector resources.
Apache 2.0 license. See LICENSE.txt for details.
