AudioRouterManager manages audio routing among the speaker / earpiece / wired headset / Bluetooth headset in scenarios such as calls, conferencing, and voice chat.
1. Get AudioRouterManager
rtcEngine.getAudioRouterManager() returns a singleton. The corresponding release method is:
2. Recommended initialization order
We recommend initializing in the following order when joining a channel / starting a call:- Get
AudioRouterManager - Set the callback listener
- Set the audio mode with
setMode(...) - Set the auto-switch policy with
setAutoChangeAudioRouter(...) - Call
init()to start route monitoring
3. Using setMode
AudioManager. Common values include:
AudioManager.MODE_NORMALAudioManager.MODE_RINGTONEAudioManager.MODE_IN_CALLAudioManager.MODE_IN_COMMUNICATIONAudioManager.MODE_CALL_SCREENINGAudioManager.MODE_CALL_REDIRECTAudioManager.MODE_COMMUNICATION_REDIRECT
Notes
- The SDK requires
minSdk = 24, so it actually takes the Android 6.0+ path. - In the 6.0+ implementation,
init()doesn’t set the audio mode automatically; you must callsetMode(...)yourself. - We therefore recommend setting the mode again before every scenario switch, for example when switching between voice chat, conferencing, and media playback.
4. Auto-switch policy with setAutoChangeAudioRouter
4.1 Simple usage
- Auto-switching is enabled
- Earpiece has priority over speaker
- Bluetooth headset has priority over wired headset
4.2 Full usage
isAutoChangetrue: audio routing is switched automatically internallyfalse: only monitor device changes without auto-switching; you callswitchAudioRouter(...)manually
isPrioritySpeakertrue: speaker has priority over earpiecefalse: earpiece has priority over speaker
isPriorityWiredEarphonetrue: wired headset has priority over Bluetooth headsetfalse: Bluetooth headset has priority over wired headset
4.3 Auto-switch rules (based on the implementation)
Auto-switching generally follows these rules:- A newly connected device usually triggers a new route selection.
- Category priority: Bluetooth headset / wired headset > earpiece / speaker.
- Priority within a group can be customized with parameters:
- Bluetooth headset vs. wired headset: controlled by
isPriorityWiredEarphone - Earpiece vs. speaker: controlled by
isPrioritySpeaker
- Bluetooth headset vs. wired headset: controlled by
- When a device is removed, an available route is reselected from the remaining devices.
- Both a Bluetooth headset and a wired headset are present:
- With
isPriorityWiredEarphone = false, the Bluetooth headset is preferred - With
isPriorityWiredEarphone = true, the wired headset is preferred
- With
- No headset-type device is present:
- With
isPrioritySpeaker = false, the earpiece is preferred - With
isPrioritySpeaker = true, the speaker is preferred
- With
5. Callbacks
5.1 Available output devices changed
- A Bluetooth headset connects / disconnects
- A wired headset is plugged in / unplugged
- The system’s available output devices change
AudioOutputDeviceType:
SPEAKER: speakerEARPIECE: earpieceWIRED_EARPHONE: wired headsetBLUETOOTH_HEADSET: Bluetooth headsetUN_KNOW: unknown / placeholder for automatic selection
The SDK requiresminSdk = 24, soAudioDeviceInfohere is usually available; it’s declared nullable only for compatibility with lower versions.
5.2 Active output device changed
5.3 Route change may produce noise
- Pause the player
- Lower the volume
- Show a prompt to the user
6. Get available devices and the active device
6.1 Get available output devices
6.2 Get the active output device
6.3 Correct the Bluetooth name (optional)
On some devices, the Bluetooth name returned byAudioDeviceInfo.getProductName() may be inaccurate. In that case, use:
To get a more accurate Bluetooth device name, make sure the Bluetooth permissions are granted; on Android 12 and later, pay attention to the BLUETOOTH_CONNECT permission.
7. Switch routes manually
In most cases, this step is triggered by the user tapping a UI button.selectType is an AudioOutputDeviceType enum value, for example:
Special value: UN_KNOW
UN_KNOW doesn’t mean switching to an “unknown device”; it means: reselect the most suitable route according to the current auto-selection policy.
Suitable for:
- The user taps “Restore the system-recommended route”
- After plugging or unplugging a device, explicitly asking the SDK to recalculate the best device
8. Release resources
We recommend releasing throughrtcEngine:
releaseAudioRouterManager() is internally equivalent to calling:
- Stops audio route monitoring
- Unregisters related broadcasts / callbacks
- Restores the audio mode to
AudioManager.MODE_NORMAL - Switches output back to the speaker
9. Common device enums
UN_KNOW: unknown / triggers automatic selectionSPEAKER: speakerEARPIECE: earpieceWIRED_EARPHONE: wired headsetBLUETOOTH_HEADSET: Bluetooth headset
10. Recommended practices
- Initialize when joining a channel and release when leaving; don’t recreate it on every button tap.
- Call
setMode(...)every time you switch app scenarios. - When the UI shows “which device is currently in use”, rely on
activeOutputDeviceChange(...)first. - When showing “which devices the user can choose”, rely on
exitOutputDeviceChange(...)orgetExitAudioOutputDevices(). - When using Bluetooth devices, verify on a real device:
- Connecting a Bluetooth headset
- Disconnecting a Bluetooth headset
- Plugging in / unplugging a wired headset
- Manually switching among speaker / earpiece / Bluetooth
- If your app has multiple audio playback/call states at the same time, focus on verifying actual behavior when switching between devices.