|
15 | 15 | import com.twilio.audioswitch.AudioSwitch;
|
16 | 16 |
|
17 | 17 | import java.util.ArrayList;
|
18 |
| -import java.util.Collections; |
19 | 18 | import java.util.List;
|
20 | 19 | import java.util.Objects;
|
21 | 20 | import java.util.Map;
|
@@ -54,8 +53,6 @@ public class AudioSwitchManager {
|
54 | 53 | @Nullable
|
55 | 54 | private AudioSwitch audioSwitch;
|
56 | 55 |
|
57 |
| - private boolean _speakerphoneOn = false; |
58 |
| - |
59 | 56 | /**
|
60 | 57 | * The audio focus mode to use while started.
|
61 |
10000
58 | *
|
@@ -105,7 +102,6 @@ public void start() {
|
105 | 102 | handler.postAtFrontOfQueue(() -> {
|
106 | 103 | if (!isActive) {
|
107 | 104 | Objects.requireNonNull(audioSwitch).activate();
|
108 |
| - audioManager.setSpeakerphoneOn(_speakerphoneOn); |
109 | 105 | isActive = true;
|
110 | 106 | }
|
111 | 107 | });
|
@@ -154,24 +150,66 @@ public void selectAudioOutput(@NonNull Class<? extends AudioDevice> audioDeviceC
|
154 | 150 | });
|
155 | 151 | }
|
156 | 152 |
|
157 |
| - public void enableSpeakerphone(boolean enable) { |
158 |
| - audioManager.setSpeakerphoneOn(enable); |
159 |
| - _speakerphoneOn = enable; |
| 153 | + private void updatePreferredDeviceList(boolean speakerOn) { |
| 154 | + preferredDeviceList = new ArrayList<>(); |
| 155 | + preferredDeviceList.add(AudioDevice.BluetoothHeadset.class); |
| 156 | + preferredDeviceList.add(AudioDevice.WiredHeadset.class); |
| 157 | + if(speakerOn) { |
| 158 | + preferredDeviceList.add(AudioDevice.Speakerphone.class); |
| 159 | + preferredDeviceList.add(AudioDevice.Earpiece.class); |
| 160 | + } else { |
| 161 | + preferredDeviceList.add(AudioDevice.Earpiece.class); |
| 162 | + preferredDeviceList.add(AudioDevice.Speakerphone.class); |
| 163 | + } |
| 164 | + handler.post(() -> { |
| 165 | + Objects.requireNonNull(audioSwitch).setPreferredDeviceList(preferredDeviceList); |
| 166 | + }); |
160 | 167 | }
|
161 | 168 |
|
162 |
| - public void enableSpeakerButPreferBluetooth() { |
163 |
| - AudioDeviceInfo bluetoothDevice = null; |
164 |
| - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { |
165 |
| - AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS); |
166 |
| - for (AudioDeviceInfo device : devices) { |
167 |
| - if (device.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) { |
168 |
| - bluetoothDevice = device; |
| 169 | + public void enableSpeakerphone(boolean enable) { |
| 170 | + updatePreferredDeviceList(enable); |
| 171 | + if(enable) { |
| 172 | + selectAudioOutput(AudioDevice.Speakerphone.class); |
| 173 | + } else { |
| 174 | + List<AudioDevice> devices = availableAudioDevices(); |
| 175 | + AudioDevice audioDevice = null; |
| 176 | + for (AudioDevice device : devices) { |
| 177 | + if (device.getClass().equals(AudioDevice.BluetoothHeadset.class)) { |
| 178 | + audioDevice = device; |
| 179 | + break; |
| 180 | + } else if(device.getClass().equals(AudioDevice.WiredHeadset.class)) { |
| 181 | + audioDevice = device; |
| 182 | + break; |
| 183 | + } else if(device.getClass().equals(AudioDevice.Earpiece.class)) { |
| 184 | + audioDevice = device; |
169 | 185 | break;
|
170 | 186 | }
|
171 | 187 | }
|
| 188 | + if (audioDevice != null) { |
| 189 | + selectAudioOutput(audioDevice.getClass()); |
| 190 | + } else { |
| 191 | + handler.post(() -> { |
| 192 | + Objects.requireNonNull(audioSwitch).selectDevice(null); |
| 193 | + }); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public void enableSpeakerButPreferBluetooth() { |
| 199 | + List<AudioDevice> devices = availableAudioDevices(); |
| 200 | + AudioDevice audioDevice = null; |
| 201 | + for (AudioDevice device : devices) { |
| 202 | + if (device.getClass().equals(AudioDevice.BluetoothHeadset.class)) { |
| 203 | + audioDevice = device; |
| 204 | + break; |
| 205 | + } else if(device.getClass().equals(AudioDevice.WiredHeadset.class)) { |
| 206 | + audioDevice = device; |
| 207 | + break; |
| 208 | + } |
172 | 209 | }
|
173 |
| - if (bluetoothDevice == null) { |
174 |
| - audioManager.setSpeakerphoneOn(_speakerphoneOn); |
| 210 | + |
| 211 | + if (audioDevice == null) { |
| 212 | + selectAudioOutput(AudioDevice.Speakerphone.class); |
175 | 213 | }
|
176 | 214 | }
|
177 | 215 |
|
|
0 commit comments