Menu

Expand
Rate this page:

Configuring Audio, Video Input and Output devices

This document refers to the 1.x version of Twilio Video JS. If you are starting with Twilio Video JS, please use the 2.x version. If you are using a 1.x version of Twilio Video JS, we recommend you to upgrade to the 2.x version.

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

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

const { connect, createLocalTracks } = require('twilio-video');

navigator.mediaDevices.enumerateDevices().then(devices => {
  const videoInput = devices.find(device => device.kind === 'videoinput');
  return createLocalTracks({ audio: true, video: { deviceId: videoInput.deviceId } });
}).then(localTracks => {
  return connect('my-token', { name: 'my-room-name', tracks: localTracks });
}).then(room => {
  console.log('Connected to room ' + room.name);
});

Audio inputs can also be selected in a similar fashion.

Selecting a Specific Audio Input

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:

const { connect, createLocalTracks } = require('twilio-video');

navigator.mediaDevices.enumerateDevices().then(devices => {
  const audioInput = devices.find(device => device.kind === 'audioinput');
  return createLocalTracks({ audio: { deviceId: audioInput.deviceId }, video: true });
}).then(localTracks => {
  return connect('my-token', { name: 'my-room-name', tracks: localTracks });
}).then(room => {
  console.log('Connected to room ' + room.name);
}); 

Selecting a Specific Audio Output

You can use navigator.mediaDevices.enumerateDevices() along with HTMLMediaElement.setSinkId() to set the audio output device like this:

const { connect } = require('twilio-video');

let audioOutputDevice;

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

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more