toggleSpeaker method
Toggles between the speakerphone, earpiece and headphones output.
Implementation
Future<void> toggleSpeaker() async {
if (PlatformUtils.isMobile) {
keepUi();
}
final List<DeviceDetails> outputs =
_currentCall.value.devices
.output()
.where((e) => e.id() != 'default' && e.deviceId() != 'default')
.toList();
if (outputs.length > 1) {
int index = outputs.indexWhere(
(e) => e == _currentCall.value.outputDevice.value,
);
if (index == -1) {
index = 0;
}
++index;
if (index >= outputs.length) {
index = 0;
}
// iOS doesn't allow to use ear-piece, when there're any Bluetooth
// devices connected.
if (PlatformUtils.isIOS) {
if (outputs.any((e) => e.speaker == AudioSpeakerKind.headphones)) {
if (outputs[index].speaker == AudioSpeakerKind.earpiece) {
++index;
if (index >= outputs.length) {
index = 0;
}
}
}
}
await _currentCall.value.setOutputDevice(outputs[index]);
}
}