Skip to contentSkip to navigationSkip to topbar
On this page

Configuring Audio, Video Input and Output devices


In this guide we'll show you how to configure audio and video input and output devices in your Twilio Video Rooms application.


Selecting a Specific Video Input

selecting-a-specific-video-input page anchor

You can use navigator.mediaDevices.enumerateDevices() to select a specific video input like this:

1
const { connect, createLocalTracks } = require('twilio-video');
2
3
navigator.mediaDevices.enumerateDevices().then(devices => {
4
const videoInput = devices.find(device => device.kind === 'videoinput');
5
return createLocalTracks({ audio: true, video: { deviceId: videoInput.deviceId } });
6
}).then(localTracks => {
7
return connect('my-token', { name: 'my-room-name', tracks: localTracks });
8
}).then(room => {
9
console.log('Connected to room ' + room.name);
10
});

Audio inputs can also be selected in a similar fashion.


Selecting a Specific Audio Input

selecting-a-specific-audio-input page anchor

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:

1
const { connect, createLocalTracks } = require('twilio-video');
2
3
navigator.mediaDevices.enumerateDevices().then(devices => {
4
const audioInput = devices.find(device => device.kind === 'audioinput');
5
return createLocalTracks({ audio: { deviceId: audioInput.deviceId }, video: true });
6
}).then(localTracks => {
7
return connect('my-token', { name: 'my-room-name', tracks: localTracks });
8
}).then(room => {
9
console.log('Connected to room ' + room.name);
10
});

Selecting a Specific Audio Output

selecting-a-specific-audio-output page anchor

You can use navigator.mediaDevices.enumerateDevices() along with HTMLMediaElement.setSinkId()(link takes you to an external page) to set the audio output device like this:

1
const { connect } = require('twilio-video');
2
3
let audioOutputDevice;
4
5
navigator.mediaDevices.enumerateDevices().then(devices => {
6
audioOutputDevice = devices.find(device => device.kind === 'audiooutput');
7
return connect('$TOKEN', { name: 'my-room-name' });
8
}).then(room => {
9
room.on('trackSubscribed', track => {
10
if (track.kind === 'audio') {
11
const audioElement = track.attach();
12
audioElement.setSinkId(audioOutputDevice.deviceId).then(() => {
13
document.body.appendChild(audioElement);
14
});
15
}
16
});
17
});

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.