如何使用 MVC 在 Web 应用程序中将音频输入从一台设备更改为另一台设备?

How can I change the audio input from one device to another in web application using MVC?

我们正在构建一个网络应用程序,用户可以在其中与其他用户进行视频聊天。我们想为他们提供一个选项,让他们可以在 PC 的内部麦克风和其他输入设备之间切换音频输入。我试着在互联网上搜索同样的内容,但没有找到任何线索。

假设您使用的是 opentok(因为您使用了 opentok 标签)。

您可以使用 OT.getDevices API 列出可用的音频输入设备。然后就可以在发布的时候传设备了。类似于:

let publisher;
function publish(audioDeviceId) {
  if (publisher) {
    publisher.destroy();
  }
  publisher = OT.initPublisher(null, {
    audioSource: audioDeviceId
  });
}

publish();

OT.getDevices((err, devices) => {
  if (!err) {
    let select = document.querySelector('select');
    devices.filter(device => device.kind === 'audioInput').forEach(device => {
      let option = document.createElement('option');
      option.value = device.deviceId;
      option.innerHTML = device.label;
      select.appendChild(option);
    });
    select.addEventListener('change', () => {
      publish(event.target.value);
    });
  }
});

您可以在 https://jsbin.com/qibaba

查看工作演示