What is the right way, during a call, to change th...
# ask-ai
f
What is the right way, during a call, to change the input devices of a user and let the LiveKit room change the stream gracefully (In React)
t
Thanks for reaching out, Carlos. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you want to switch mic/camera during an active call in React and have the stream update smoothly. Two supported approaches: • Room-level (simplest, handles hot-swap for published tracks): a. Get the selected
deviceId
and
kind
(
'audioinput'
or
'videoinput'
). b. Call:
Copy code
await room.switchActiveDevice(kind, deviceId)
This updates the corresponding local track and republishing is handled for you. Reference • Track-level (fine-grained control): ◦ If you have the `LocalTrack`:
Copy code
await localTrack.setDeviceId(deviceId)
• Or hot-swap explicitly:
Copy code
await localParticipant.replaceTrack(localTrack, { /* options if needed */ })
Reference If you’re using
@livekit/react
, I can share a snippet wired to a device selector—do you prefer the room-level helper or track-level control? Sources: Web Applications & Integration Examples | livekit/livekit_composite | DeepW... | JavaScript SDK & React Components | livekit/livekit_composite | DeepWiki
f
Yes, please
t
You confirmed you want a React snippet to switch devices during a call. Below is a simple room-level example using
room.switchActiveDevice
, which hot-swaps the published track smoothly.
Copy code
import { useEffect, useState } from 'react';
import { useRoom } from '@livekit/components-react';

const kindMap = {
  'video-input': 'videoinput',
  'audio-input': 'audioinput',
  'audio-output': 'audiooutput',
} as const;

export function DeviceSelectors() {
  const { room } = useRoom();
  const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);

  useEffect(() => {
    const load = async () => {
      const list = await navigator.mediaDevices.enumerateDevices();
      setDevices(list);
    };
    load();
    navigator.mediaDevices.addEventListener('devicechange', load);
    return () => navigator.mediaDevices.removeEventListener('devicechange', load);
  }, []);

  const onChange = async (e: React.ChangeEvent<HTMLSelectElement>) => {
    const deviceId = e.target.value;
    const kind = kindMap[e.target.name as keyof typeof kindMap];
    if (room && deviceId && kind) {
      await room.switchActiveDevice(kind, deviceId);
    }
  };

  const options = (kind: MediaDeviceKind) =>
    devices.filter(d => d.kind === kind).map(d => (
      <option key={d.deviceId} value={d.deviceId}>{d.label || d.deviceId}</option>
    ));

  return (
    <div>
      <label>
        Camera
        <select name="video-input" onChange={onChange}>{options('videoinput')}</select>
      </label>
      <label>
        Microphone
        <select name="audio-input" onChange={onChange}>{options('audioinput')}</select>
      </label>
      <label>
        Speakers
        <select name="audio-output" onChange={onChange}>{options('audiooutput')}</select>
      </label>
    </div>
  );
}
• This calls
room.switchActiveDevice(kind, deviceId)
to swap mic/camera/speakers smoothly. Reference If you’d prefer track-level control (using
LocalTrack.setDeviceId
), I can share that variant too. Sources: Web Applications & Integration Examples | livekit/livekit_composite | DeepW... | JavaScript SDK & React Components | livekit/livekit_composite | DeepWiki | React quickstart | LiveKit docs | React Core and Components
f
The idea is to, during the call, let the user change individually their microphone, their camera, or the audio device (Like in normal video calls)
t
You’re looking to let users switch mic, camera, or speakers individually during a call—like standard video apps. The snippet I shared supports exactly that via per-select changes calling
room.switchActiveDevice
. Key actions: • Microphone: select →
await room.switchActiveDevice('audioinput', deviceId)
• Camera: select →
await room.switchActiveDevice('videoinput', deviceId)
• Speakers: select →
await room.switchActiveDevice('audiooutput', deviceId)
This hot-swaps the published track/output smoothly for the chosen device. If you’d like, I can adapt the snippet to your UI or show the track-level variant using
LocalTrack.setDeviceId
for finer control. Reference