Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Configuring Audio, Video Input and Output devices


In a Twilio Video Rooms application, you select audio and video input and output devices by enumerating the available devices with navigator.mediaDevices.enumerateDevices() and passing the chosen device ID to your local tracks.


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:

Select a specific video input device

select-a-specific-video-input-device page anchor
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:

Select a specific audio input device

select-a-specific-audio-input-device page anchor
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:

Select a specific audio output device

select-a-specific-audio-output-device page anchor
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
});