Skip to content

Commit 7931e77

Browse files
committed
android: support chooseAudioRoute() and onAudioDeviceChanged event
** chooseAudioRoute() let user choose their own audio route param: "EARPIECE", "SPEAKER_PHONE", "WIRED_HEADSET", "BLUETOOTH" if a audio device is unavailable, it will do nothing. if success, it will return a promise contains an object like `onAudioDeviceChanged`'s data below. ** onAudioDeviceChanged Event Will fire event to JS, indicating current available audio devices. and current selected audio devices. your UI can display available options according to these data. Note for writableArray issue, `availableAudioDeviceList` is a JSON Array String so you need to `JSON.parse()` it before you use it as an array object in js example: ``` { "availableAudioDeviceList": "[\"EARPIECE\", \"SPEAKER_PHONE\"]", "selectedAudioDevice": "SPEAKER_PHONE", } ```
1 parent 335540e commit 7931e77

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

android/src/main/java/com/zxcpoiu/incallmanager/InCallManagerModule.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,22 @@ public void requestCameraPermission(Promise promise) {
14681468
}
14691469
}
14701470

1471+
@ReactMethod
1472+
public void chooseAudioRoute(String audioRoute, Promise promise) {
1473+
Log.d(TAG, "RNInCallManager.chooseAudioRoute(): user choose audioDevice = " + audioRoute);
1474+
1475+
if (audioRoute.equals(AudioDevice.EARPIECE.name())) {
1476+
selectAudioDevice(AudioDevice.EARPIECE);
1477+
} else if (audioRoute.equals(AudioDevice.SPEAKER_PHONE.name())) {
1478+
selectAudioDevice(AudioDevice.SPEAKER_PHONE);
1479+
} else if (audioRoute.equals(AudioDevice.WIRED_HEADSET.name())) {
1480+
selectAudioDevice(AudioDevice.WIRED_HEADSET);
1481+
} else if (audioRoute.equals(AudioDevice.BLUETOOTH.name())) {
1482+
selectAudioDevice(AudioDevice.BLUETOOTH);
1483+
}
1484+
promise.resolve(getAudioDeviceStatusMap());
1485+
}
1486+
14711487
private void _requestPermission(String targetPermission, Promise promise) {
14721488
Activity currentActivity = getCurrentActivity();
14731489
if (currentActivity == null) {
@@ -1644,7 +1660,8 @@ public void setDefaultAudioDevice(AudioDevice defaultDevice) {
16441660
/** Changes selection of the currently active audio device. */
16451661
public void selectAudioDevice(AudioDevice device) {
16461662
if (!audioDevices.contains(device)) {
1647-
Log.e(TAG, "Can not select " + device + " from available " + audioDevices);
1663+
Log.e(TAG, "selectAudioDevice() Can not select " + device + " from available " + audioDevices);
1664+
return;
16481665
}
16491666
userSelectedAudioDevice = device;
16501667
updateAudioDeviceState();
@@ -1870,7 +1887,26 @@ public void updateAudioDeviceState() {
18701887
audioManagerEvents.onAudioDeviceChanged(selectedAudioDevice, audioDevices);
18711888
}
18721889
*/
1890+
sendEvent("onAudioDeviceChanged", getAudioDeviceStatusMap());
18731891
}
18741892
Log.d(TAG, "--- updateAudioDeviceState done");
18751893
}
1894+
1895+
private WritableMap getAudioDeviceStatusMap() {
1896+
WritableMap data = Arguments.createMap();
1897+
String audioDevicesJson = "[";
1898+
for (AudioDevice s: audioDevices) {
1899+
audioDevicesJson += "\"" + s.name() + "\",";
1900+
}
1901+
// --- strip the last `,`
1902+
if (audioDevicesJson.length() > 1) {
1903+
audioDevicesJson = audioDevicesJson.substring(0, audioDevicesJson.length() - 1);
1904+
}
1905+
audioDevicesJson += "]";
1906+
1907+
data.putString("availableAudioDeviceList", audioDevicesJson);
1908+
data.putString("selectedAudioDevice", selectedAudioDevice.name());
1909+
1910+
return data;
1911+
}
18761912
}

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ class InCallManager {
171171
}
172172
}
173173
}
174+
175+
async chooseAudioRoute(route) {
176+
let result = await _InCallManager.chooseAudioRoute(route);
177+
return result;
178+
}
174179
}
175180

176181
export default new InCallManager();

0 commit comments

Comments
 (0)