In this guide we'll show you how to configure audio and video input and output devices in your Twilio Video Rooms application.
You can use navigator.mediaDevices.enumerateDevices()
to select a specific video input like this:
1const { connect, createLocalTracks } = require('twilio-video');23navigator.mediaDevices.enumerateDevices().then(devices => {4const videoInput = devices.find(device => device.kind === 'videoinput');5return createLocalTracks({ audio: true, video: { deviceId: videoInput.deviceId } });6}).then(localTracks => {7return connect('my-token', { name: 'my-room-name', tracks: localTracks });8}).then(room => {9console.log('Connected to room ' + room.name);10});
Audio inputs can also be selected in a similar fashion.
Selecting a specific audio input is the same as selecting a specific video input. Just select the audio input device ID and set it on the constraints:
1const { connect, createLocalTracks } = require('twilio-video');23navigator.mediaDevices.enumerateDevices().then(devices => {4const audioInput = devices.find(device => device.kind === 'audioinput');5return createLocalTracks({ audio: { deviceId: audioInput.deviceId }, video: true });6}).then(localTracks => {7return connect('my-token', { name: 'my-room-name', tracks: localTracks });8}).then(room => {9console.log('Connected to room ' + room.name);10});
You can use navigator.mediaDevices.enumerateDevices()
along with HTMLMediaElement.setSinkId() to set the audio output device like this:
1const { connect } = require('twilio-video');23let audioOutputDevice;45navigator.mediaDevices.enumerateDevices().then(devices => {6audioOutputDevice = devices.find(device => device.kind === 'audiooutput');7return connect('$TOKEN', { name: 'my-room-name' });8}).then(room => {9room.on('trackSubscribed', track => {10if (track.kind === 'audio') {11const audioElement = track.attach();12audioElement.setSinkId(audioOutputDevice.deviceId).then(() => {13document.body.appendChild(audioElement);14});15}16});17});