Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Configuring Audio, Video Input and Output devices


(warning)

Warning

This page is for reference only. We are no longer onboarding new customers to Programmable Video. Existing customers can continue to use the product until December 5, 2024(link takes you to an external page).
We recommend migrating your application to the API provided by our preferred video partner, Zoom. We've prepared this migration guide(link takes you to an external page) to assist you in minimizing any service disruption.

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:


_10
const { connect, createLocalTracks } = require('twilio-video');
_10
_10
navigator.mediaDevices.enumerateDevices().then(devices => {
_10
const videoInput = devices.find(device => device.kind === 'videoinput');
_10
return createLocalTracks({ audio: true, video: { deviceId: videoInput.deviceId } });
_10
}).then(localTracks => {
_10
return connect('my-token', { name: 'my-room-name', tracks: localTracks });
_10
}).then(room => {
_10
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:


_10
const { connect, createLocalTracks } = require('twilio-video');
_10
_10
navigator.mediaDevices.enumerateDevices().then(devices => {
_10
const audioInput = devices.find(device => device.kind === 'audioinput');
_10
return createLocalTracks({ audio: { deviceId: audioInput.deviceId }, video: true });
_10
}).then(localTracks => {
_10
return connect('my-token', { name: 'my-room-name', tracks: localTracks });
_10
}).then(room => {
_10
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:


_17
const { connect } = require('twilio-video');
_17
_17
let audioOutputDevice;
_17
_17
navigator.mediaDevices.enumerateDevices().then(devices => {
_17
audioOutputDevice = devices.find(device => device.kind === 'audiooutput');
_17
return connect('$TOKEN', { name: 'my-room-name' });
_17
}).then(room => {
_17
room.on('trackSubscribed', track => {
_17
if (track.kind === 'audio') {
_17
const audioElement = track.attach();
_17
audioElement.setSinkId(audioOutputDevice.deviceId).then(() => {
_17
document.body.appendChild(audioElement);
_17
});
_17
}
_17
});
_17
});


Rate this page: