changeMic method

Future<void> changeMic({
  1. String? deviceId,
  2. CustomStreamTrack? customMicrophoneTrack,
})

Switches the mic to customMicrophoneTrack (unmutes), to the device deviceId, or to the next mic when both are null. A device switch keeps the mute state. An unknown deviceId throws 3059. Outside a room a deviceId is selected and applied on the next join.

Implementation

Future<void> changeMic({
  String? deviceId,
  CustomStreamTrack? customMicrophoneTrack,
}) async {
  try {
    if (!_joined &&
        !_joinReturned &&
        deviceId != null &&
        customMicrophoneTrack == null) {
      // The engine's changeMic needs a joined room; a device picked
      // before join (or after a leave) is only a selection, applied on join.
      await _backend.setAudioDevice(deviceId);
    } else {
      await _backend.changeMic(
        customMicrophoneTrack == null ? deviceId : null,
        customTrack: customMicrophoneTrack,
      );
    }
    if (customMicrophoneTrack != null) _micEnabled = true;
    if (deviceId != null && customMicrophoneTrack == null) {
      await _selectMic(deviceId);
    }
  } catch (_) {
    await customMicrophoneTrack?.stop();
    rethrow;
  }
}