Skip to content

Instantly share code, notes, and snippets.

@alizeec
Created August 6, 2020 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alizeec/098295222a1bae1746477a80d39f0b05 to your computer and use it in GitHub Desktop.
Save alizeec/098295222a1bae1746477a80d39f0b05 to your computer and use it in GitHub Desktop.
AudioService.kt
class AudioService {
private fun startAudioDevicesWatch(onListChange: (AudioOutput) -> Unit) {
audioDeviceSelector.start { list, selectedDevice ->
println("[AUDIOSDK] when list of devices change selectedAudioDevice = ${selectedDevice?.name} audioDevices = $list")
onListChange(selectedDevice?.let { getAudioOutput(selectedDevice, true) } ?: AudioOutput(AudioOutputType.PHONE, null, true))
}
}
private fun getAudioOutput(audioDevice: AudioDevice, isSelected: Boolean): AudioOutput {
println("[AUDIOSDK] device ${audioDevice.name} is selected ?$isSelected")
return when (audioDevice) {
is AudioDevice.BluetoothHeadset -> AudioOutput(AudioOutputType.BLUETOOTH, audioDevice.name, isSelected)
is AudioDevice.WiredHeadset -> AudioOutput(AudioOutputType.WIRED_HEADSET, null, isSelected)
is AudioDevice.Earpiece -> AudioOutput(AudioOutputType.PHONE, null, isSelected)
is AudioDevice.Speakerphone -> AudioOutput(AudioOutputType.SPEAKER, null, isSelected)
}
}
override fun getAvailableAudioDevices(): List<AudioOutput> {
val selected = audioDeviceSelector.selectedAudioDevice
println("[AUDIOSDK] getAvailableAudioDevices audioDeviceSelector.selectedAudioDevice = $selected")
return audioDeviceSelector.availableAudioDevices.map { getAudioOutput(it, it == selected) }
}
override fun getCurrentAudioOutput(): AudioOutput {
val audioDevice = audioDeviceSelector.selectedAudioDevice
println("[AUDIOSDK] fetch current audio output audioDeviceSelector.selectedAudioDevice = $audioDevice")
return audioDevice?.let { getAudioOutput(it, true) } ?: AudioOutput(AudioOutputType.PHONE, null, true)
}
private fun selectAudioDevice(type: AudioOutputType): AudioOutput {
val availableAudioDevices = audioDeviceSelector.availableAudioDevices
println("[AUDIOSDK] user action: switch to $type")
when (type) {
AudioOutputType.BLUETOOTH -> availableAudioDevices.find { it is AudioDevice.BluetoothHeadset }
?.let { audioDeviceSelector.selectDevice(it) }
AudioOutputType.SPEAKER -> availableAudioDevices.find { it is AudioDevice.Speakerphone }
?.let { audioDeviceSelector.selectDevice(it) }
AudioOutputType.PHONE -> availableAudioDevices.find { it is AudioDevice.Earpiece }
?.let { audioDeviceSelector.selectDevice(it) }
AudioOutputType.WIRED_HEADSET -> availableAudioDevices.find { it is AudioDevice.WiredHeadset }
?.let { audioDeviceSelector.selectDevice(it) }
}
try {
audioDeviceSelector.activate()
} catch (error: IllegalStateException){
Timber.e("Audio output cannot be activated because it's not ready")
}
logAudioInfo(EVENT_SWITCH_OUTPUT)
return getCurrentAudioOutput()
}
private fun stopAudioDevicesWatch() {
audioDeviceSelector.stop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment