|
1 |
| -# audioswitch |
2 |
| -An Android audio management library for building real-time communication apps. |
| 1 | +# AudioSwitch |
| 2 | + |
| 3 | +[](https://circleci.com/gh/twilio/audioswitch) |
| 4 | + |
| 5 | +An Android audio management library for real-time communication apps. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- [x] Manage [audio focus](https://developer.android.com/guide/topics/media-apps/audio-focus) for typical VoIP and Video conferencing use cases. |
| 12 | +- [x] Manage audio input and output devices. |
| 13 | + - [x] Detect changes in available audio devices |
| 14 | + - [x] Enumerate audio devices |
| 15 | + - [x] Select an audio device |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +Android Studio Version | Android API Version Min |
| 20 | +------------ | ------------- |
| 21 | +3.6+ | 16 |
| 22 | + |
| 23 | +## Getting Started |
| 24 | + |
| 25 | +To get started using this library, follow the steps below. |
| 26 | + |
| 27 | +### AudioDeviceSelector Setup |
| 28 | +Instantiate an instance of the [AudioDeviceSelector](audioswitch/src/main/java/com/twilio/audioswitch/selection/AudioDeviceSelector.kt) class, passing a reference to the application context. |
| 29 | + |
| 30 | +``` |
| 31 | +val audioDeviceSelector = AudioDeviceSelector(applicationContext) |
| 32 | +``` |
| 33 | + |
| 34 | +### Listen for Devices |
| 35 | +To begin listening for live audio device changes, call the start function and pass a lambda that will receive [AudioDevices](audioswitch/src/main/java/com/twilio/audioswitch/selection/AudioDevice.kt) when they become available. |
| 36 | + |
| 37 | +``` |
| 38 | +audioDeviceSelector.start { audioDevices, selectedDevice -> |
| 39 | + // TODO update UI with audio devices |
| 40 | +} |
| 41 | +``` |
| 42 | +You can also retrieve the available and selected audio devices manually at any time by calling the following properties: |
| 43 | +``` |
| 44 | +val devices: List<AudioDevice> = audioDeviceSelector.availableAudioDevices |
| 45 | +val selectedDevice: AudioDevice? = audioDeviceSelector.selectedAudioDevice |
| 46 | +``` |
| 47 | +**Note:** Don't forget to stop listening for audio devices when no longer needed in order to prevent a memory leak. |
| 48 | +``` |
| 49 | +audioDeviceSelector.stop() |
| 50 | +``` |
| 51 | + |
| 52 | +### Select a Device |
| 53 | +Before activating an AudioDevice, it needs to be selected first. |
| 54 | +``` |
| 55 | +devices.find { it is AudioDevice.Speakerphone }?.let { audioDeviceSelector.selectDevice(it) } |
| 56 | +``` |
| 57 | +If no device is selected, then the library will automatically select a device based on the following priority: `BluetoothHeadset -> WiredHeadset -> Earpiece -> Speakerphone`. |
| 58 | + |
| 59 | +### Activate a Device |
| 60 | +Activating a device acquires audio focus with [voice communication usage](https://developer.android.com/reference/android/media/AudioAttributes#USAGE_VOICE_COMMUNICATION) and begins routing audio input/output to the selected device. |
| 61 | +``` |
| 62 | +audioDeviceSelector.activate() |
| 63 | +``` |
| 64 | +Make sure to revert back to the prior audio state when it makes sense to do so in your app. |
| 65 | +``` |
| 66 | +audioDeviceSelector.deactivate() |
| 67 | +``` |
| 68 | +**Note:** The `stop()` function will call `deactivate()` before closing AudioDeviceSelector resources. |
| 69 | + |
| 70 | +## License |
| 71 | + |
| 72 | +Apache 2.0 license. See [LICENSE.txt](LICENSE.txt) for details. |
0 commit comments