> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Audio Modes

> CometChat Calling SDK v5 - Audio Modes for Flutter

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:

```dart theme={null}
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:

```dart theme={null}
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`:

```dart theme={null}
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) {},
));
```

<Warning>
  Flutter listeners are not lifecycle-aware. You must manually remove the media events listener in your widget's `dispose()` method to prevent memory leaks.
</Warning>

## Hide Audio Mode Button

To prevent users from changing the audio mode, hide the button in the call UI:

```dart theme={null}
SessionSettings sessionSettings = CometChatCalls.SessionSettingsBuilder()
    .setAudioMode(AudioMode.speaker) // Fixed audio mode
    .hideAudioModeButton(true)       // Hide toggle button
    .build();
```

<Note>
  The SDK automatically detects connected audio devices. If a Bluetooth device is connected, it becomes available as an audio mode option.
</Note>
