在获得许可之前,MediaStream相机和麦克风标签?

时间:2017-10-11 01:02:05

标签: javascript ecmascript-6 webrtc webcam-capture mediastream

让这种code工作是微不足道的:

const videoTracks = this.localStream.getVideoTracks();
const audioTracks = this.localStream.getAudioTracks();
if (videoTracks.length > 0)
  this.trace('Using video device: ' + videoTracks[0].label);
if (audioTracks.length > 0)
  this.trace('Using audio device: ' + audioTracks[0].label);

在选择设备之前怎么样?

  navigator.mediaDevices
    .enumerateDevices()
    .then(this.gotDevices.bind(this))
    .catch(this.alertsService.add.bind(this.alertsService));
}

gotDevices(deviceInfos: MediaDeviceInfo[]) {
    this.mediaDeviceInfos = deviceInfos;
    this.mediaDeviceInfos.forEach(device => {
        if (device.kind === 'audioinput')
          this.mics.push(device);
        else if (device.kind === 'videoinput')
          this.cams.push(device);
        else
          console.warn(`Unexpected: ${JSON.stringify(device)}`);
    });
}

this.mediaDeviceInfos.some(dev => dev.label)false。所以我必须在我的输入中使用dev.deviceId,这很难看:

camera choice screen

我需要这里选择的特定设备,因为我这样使用它,支持多个凸轮和麦克风:

const constraints: MediaStreamConstraints = {
  audio: { advanced: [{ deviceId: mic.deviceId, groupId: mic.groupId }] },
  video: { advanced: [{ deviceId: cam.deviceId, groupId: cam.groupId }] }
};

如何获取相机和麦克风的标签,以便用户可以选择主题?

1 个答案:

答案 0 :(得分:1)

您可以在public static void main(String[] args) { int day; int num; int newDay; String [] days = new String[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; System.out.println("Enter the day and the number to add"); Scanner scnr = new Scanner(System.in); day = scnr.nextInt(); num = scnr.nextInt(); newDay = (num + day) % 7; System.out.println(newDay); System.out.println("The new day is " + days[newDay - 1] ); } 内将navigator.mediaDevices.enumerateDevices().then()联系起来navigator.mediaDevices.getUserMedia()。如果协议为https:并且已授予设备或设备权限,则应将MediaDeviceInfo对象"label"属性设置为可用的媒体设备。

navigator.mediaDevices.getUserMedia(/* {video:true, audio:true} */)
.then(stream => 
    navigator.mediaDevices
    .enumerateDevices()
    .then(devices => {
      return devices
    })
    .catch(err => {throw err})
)
.then(devices => {
  console.log(devices);
  for (const mediaDeviceInfo of devices) {
    const {deviceId, kind, label} = mediaDeviceInfo;
      console.log(deviceId, kind, label);
  }
})
.catch(err => console.error(err))