Audio Device Management
Update time: 2025/06/11 16:45:39
In an audio scenario, you may need to choose appropriate audio collection and playback device based on the actual site conditions. For example, you need to use a receiver for audio playback in an indoor scene. The NERTC SDK supports free switching of audio devices.
Considerations
When an earphone is connected, the earphone is automatically used for playback. In this case, even if a speaker or receiver is set as the audio playback device, the earphone is still used for playback purpose. Once the earphone is disconnected, the configuration for audio playback device will apply.
Method
You can take the following steps to switch audio routing in your project:
-
After successfully joining a room, you can call
isSpeakerphoneOnto determine whether it is in the receiver or speaker mode. -
You can call the API
setSpeakerphoneOn(boolean enable)for switching purpose.- true: Speaker.
- false: Receiver.
-
You can listen for audio routing switching events by listening for the callback of
onAudioDeviceChanged.
Sample code
// Determine the current audio router.
boolean isSpeakerphoneOn = NERtcEx.getInstance().isSpeakerphoneOn();
// Set the audio router.
NERtcEx.getInstance().setSpeakerphoneOn(isSpeakerphoneOn);
// Audio router switching callback.
public void onAudioDeviceChanged(int selected) {
String audioDevice;
switch (selected) {
case NERtcConstants.AudioDevice.EARPIECE:
audioDevice = "Receiver";
break;
case NERtcConstants.AudioDevice.BLUETOOTH_HEADSET:
audioDevice = "Bluetooth earphone";
break;
case NERtcConstants.AudioDevice.SPEAKER_PHONE:
audioDevice = "Speaker";
break;
case NERtcConstants.AudioDevice.WIRED_HEADSET:
audioDevice = "Wired earphone";
break;
default:
audioDevice = "EARPIECE";
break;
}
}
API reference
| Method | Function description |
|---|---|
| setPlayoutDeviceMute | Used to enable/disable mute for audio playback. |
| setRecordDeviceMute | Used to enable/disable mute for audio collection. |
| isSpeakerphoneOn | Check whether the speaker is turned on or off. |
| setSpeakerphoneOn | Used to turn on or off the speaker. |
Audio device callback:
| Event | Function description |
|---|---|
| onAudioDeviceChanged | Audio playback device changed. |





