获得这种 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 ,这很难看:

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

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

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

请您参考如下方法:

您可以在链接到 navigator.mediaDevices.getUserMedia().then() 中调用 navigator.mediaDevices.enumerateDevices()。当协议(protocol)为 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)) 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!