Control audio output routing during calls. Switch between speaker, earpiece, Bluetooth, and wired headphones based on user preference or device availability.
Available Audio Modes
| Mode | Description |
|---|
SPEAKER | Routes audio through the device loudspeaker |
EARPIECE | Routes audio through the phone earpiece (for private calls) |
BLUETOOTH | Routes audio through a connected Bluetooth device |
HEADPHONES | Routes audio through wired headphones |
Set Initial Audio Mode
Configure the audio mode when joining a session:
val sessionSettings = CometChatCalls.SessionSettingsBuilder()
.setAudioMode(AudioMode.SPEAKER)
.build()
CometChatCalls.joinSession(sessionId, sessionSettings, callViewContainer,
object : CometChatCalls.CallbackListener<CallSession>() {
override fun onSuccess(callSession: CallSession) {
Log.d(TAG, "Joined with speaker mode")
}
override fun onError(e: CometChatException) {
Log.e(TAG, "Failed: ${e.message}")
}
}
)
SessionSettings sessionSettings = new CometChatCalls.SessionSettingsBuilder()
.setAudioMode(AudioMode.SPEAKER)
.build();
CometChatCalls.joinSession(sessionId, sessionSettings, callViewContainer,
new CometChatCalls.CallbackListener<CallSession>() {
@Override
public void onSuccess(CallSession callSession) {
Log.d(TAG, "Joined with speaker mode");
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "Failed: " + e.getMessage());
}
}
);
Change Audio Mode During Call
Switch audio modes dynamically during an active call:
val callSession = CallSession.getInstance()
// Switch to speaker
callSession.setAudioMode(AudioMode.SPEAKER)
// Switch to earpiece
callSession.setAudioMode(AudioMode.EARPIECE)
// Switch to Bluetooth
callSession.setAudioMode(AudioMode.BLUETOOTH)
// Switch to wired headphones
callSession.setAudioMode(AudioMode.HEADPHONES)
CallSession callSession = CallSession.getInstance();
// Switch to speaker
callSession.setAudioMode(AudioMode.SPEAKER);
// Switch to earpiece
callSession.setAudioMode(AudioMode.EARPIECE);
// Switch to Bluetooth
callSession.setAudioMode(AudioMode.BLUETOOTH);
// Switch to wired headphones
callSession.setAudioMode(AudioMode.HEADPHONES);
Listen for Audio Mode Changes
Monitor audio mode changes using MediaEventsListener:
val callSession = CallSession.getInstance()
callSession.addMediaEventsListener(this, object : MediaEventsListener() {
override fun onAudioModeChanged(audioMode: AudioMode) {
when (audioMode) {
AudioMode.SPEAKER -> Log.d(TAG, "Switched to speaker")
AudioMode.EARPIECE -> Log.d(TAG, "Switched to earpiece")
AudioMode.BLUETOOTH -> Log.d(TAG, "Switched to Bluetooth")
AudioMode.HEADPHONES -> Log.d(TAG, "Switched to headphones")
}
// Update audio mode button icon
updateAudioModeIcon(audioMode)
}
// Other callbacks...
override fun onAudioMuted() {}
override fun onAudioUnMuted() {}
override fun onVideoPaused() {}
override fun onVideoResumed() {}
override fun onRecordingStarted() {}
override fun onRecordingStopped() {}
override fun onScreenShareStarted() {}
override fun onScreenShareStopped() {}
override fun onCameraFacingChanged(facing: CameraFacing) {}
})
CallSession callSession = CallSession.getInstance();
callSession.addMediaEventsListener(this, new MediaEventsListener() {
@Override
public void onAudioModeChanged(AudioMode audioMode) {
switch (audioMode) {
case SPEAKER:
Log.d(TAG, "Switched to speaker");
break;
case EARPIECE:
Log.d(TAG, "Switched to earpiece");
break;
case BLUETOOTH:
Log.d(TAG, "Switched to Bluetooth");
break;
case HEADPHONES:
Log.d(TAG, "Switched to headphones");
break;
}
// Update audio mode button icon
updateAudioModeIcon(audioMode);
}
// Other callbacks...
});
To prevent users from changing the audio mode, hide the button in the call UI:
val sessionSettings = CometChatCalls.SessionSettingsBuilder()
.setAudioMode(AudioMode.SPEAKER) // Fixed audio mode
.hideAudioModeButton(true) // Hide toggle button
.build()
SessionSettings sessionSettings = new CometChatCalls.SessionSettingsBuilder()
.setAudioMode(AudioMode.SPEAKER) // Fixed audio mode
.hideAudioModeButton(true) // Hide toggle button
.build();
The SDK automatically detects connected audio devices. If Bluetooth or wired headphones are connected, they become available as audio mode options.