Control audio output routing during calls. Switch between speaker, earpiece, and Bluetooth 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 |
Set Initial Audio Mode
Configure the audio mode when joining a session:
SessionSettings sessionSettings = CometChatCalls.SessionSettingsBuilder()
.setAudioMode(AudioMode.speaker)
.build();
CometChatCalls.joinSession(
sessionId: sessionId,
sessionSettings: sessionSettings,
onSuccess: (Widget? callWidget) {
debugPrint("Joined with speaker mode");
// Place callWidget in your widget tree
},
onError: (CometChatCallsException e) {
debugPrint("Failed: ${e.message}");
},
);
Change Audio Mode During Call
Switch audio modes dynamically during an active call:
CallSession? callSession = CallSession.getInstance();
// Switch to speaker
await callSession?.setAudioModeType(AudioMode.speaker);
// Switch to earpiece
await callSession?.setAudioModeType(AudioMode.earpiece);
// Switch to Bluetooth
await callSession?.setAudioModeType(AudioMode.bluetooth);
Listen for Audio Mode Changes
Monitor audio mode changes using MediaEventsListener:
CallSession? callSession = CallSession.getInstance();
callSession?.addMediaEventsListener(MediaEventsListener(
onAudioModeChanged: (AudioMode audioMode) {
switch (audioMode) {
case AudioMode.speaker:
debugPrint("Switched to speaker");
break;
case AudioMode.earpiece:
debugPrint("Switched to earpiece");
break;
case AudioMode.bluetooth:
debugPrint("Switched to Bluetooth");
break;
}
// Update audio mode button icon
updateAudioModeIcon(audioMode);
},
// Other callbacks...
onAudioMuted: () {},
onAudioUnMuted: () {},
onVideoPaused: () {},
onVideoResumed: () {},
onRecordingStarted: () {},
onRecordingStopped: () {},
onScreenShareStarted: () {},
onScreenShareStopped: () {},
onCameraFacingChanged: (CameraFacing facing) {},
));
Flutter listeners are not lifecycle-aware. You must manually remove the media events listener in your widget’s dispose() method to prevent memory leaks.
To prevent users from changing the audio mode, hide the button in the call UI:
SessionSettings sessionSettings = CometChatCalls.SessionSettingsBuilder()
.setAudioMode(AudioMode.speaker) // Fixed audio mode
.hideAudioModeButton(true) // Hide toggle button
.build();
The SDK automatically detects connected audio devices. If a Bluetooth device is connected, it becomes available as an audio mode option.