Евгений
Евгений

Reputation: 11

How to programmatically select a microphone in google chrome?

Example PICTURE

Can I somehow programmatically select a mirophone in chrome? Provided that there is full access to the machine on which the browser is launched.

Can it be somehow done on JavaScipt? If this can be implemented on JS, then i can make an extension, via tampermonkey or run the script via the webdriver Selenium.

I found the following example, which displays a list of audio devices in the console. Can I also change the microphone through the console? Give an example, if possible. If this is not possible, write down why this will not work and how you can try to solve the problem in different ways.

navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
  devices.forEach(function(device) {
    console.log(device.kind + ": " + device.label +
            " id = " + device.deviceId);
  });
})

Upvotes: 1

Views: 903

Answers (1)

Ale
Ale

Reputation: 1032

I was searching for something similar and I have found this material from google that I think works for your case. Below is quote from it.

navigator.mediaDevices.enumerateDevices().then((devices) => {
  devices = devices.filter((d) => d.kind === 'audioinput');
});

You can then pass the deviceId that you wish to use when you call getUserMedia.

navigator.mediaDevices.getUserMedia({
  audio: {
    deviceId: devices[0].deviceId
  }
});

Upvotes: 2

Related Questions