Javascript webrtc在chrome 54中默认使用前置摄像头

Javascript webrtc在chrome 54中默认使用前置摄像头,javascript,android,google-chrome,camera,webrtc,Javascript,Android,Google Chrome,Camera,Webrtc,伙计们,我一直在尝试使用webrtc访问chrome的前后摄像头,它工作得很好,我能够在前后摄像头之间切换。但是,默认情况下,chrome会拾取前置摄像头,当浏览器打开时,有没有办法让chrome拾取后置摄像头 var videoSelect = document.querySelector('select#videoSource'); var selectors = [videoSelect]; function gotDevices(deviceInfos) { // Handles

伙计们,我一直在尝试使用webrtc访问chrome的前后摄像头,它工作得很好,我能够在前后摄像头之间切换。但是,默认情况下,chrome会拾取前置摄像头,当浏览器打开时,有没有办法让chrome拾取后置摄像头

var videoSelect = document.querySelector('select#videoSource');
var selectors = [videoSelect];

function gotDevices(deviceInfos) {
  // Handles being called several times to update labels. Preserve values.
  var values = selectors.map(function(select) {
    return select.value;
  });
  selectors.forEach(function(select) {
    while (select.firstChild) {
      select.removeChild(select.firstChild);
    }
  });
  for (var i = 0; i !== deviceInfos.length; ++i) {
    var deviceInfo = deviceInfos[i];
    var option = document.createElement('option');
    option.value = deviceInfo.deviceId;
   if (deviceInfo.kind === 'videoinput') {
      option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1);
      videoSelect.appendChild(option);
    } else {
      console.log('Some other kind of source/device: ', deviceInfo);
    }
  }
  selectors.forEach(function(select, selectorIndex) {
    if (Array.prototype.slice.call(select.childNodes).some(function(n) {
      return n.value === values[selectorIndex];
    })) {
      select.value = values[selectorIndex];
    }
  });
}

navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
我尝试按降序访问阵列,以首先获取后摄像头,但没有帮助

for (var  i = deviceInfos.length - 1; i >= 0; i--) {
    var deviceInfo = deviceInfos[i];
    var option = document.createElement('option');
    option.value = deviceInfo.deviceId;
           ....... //code follows
更新:我也尝试将这些线添加到上述代码中,但同样的响应,默认情况下打开了前凸轮

for (var i = 0; i !== deviceInfos.length; ++i) {
    var deviceInfo = deviceInfos[i];


   if (deviceInfo.kind === 'videoinput' && deviceInfo.facing == "environment") {
    videoSourceId = deviceInfo.id;


    } else {
      console.log('Some other kind of source/device: ', deviceInfo);
    }
  }
//other code

var constraints = {
    video: { optional: [{sourceId: videoSourceId}] }
  };
  navigator.mediaDevices.getUserMedia(constraints).
      then(gotStream).then(gotDevices).catch(handleError);
}
PS:我尝试在其他代码中使用facingMode:“environment”,但这里是否有这样的范围?

使用约束如何

对于后置摄像头:

{audio:false,video:{facingMode:{exact:"environment"}}}
对于前摄像头:

{audio:false,video:{facingMode:"user"}}
使用约束如何

对于后置摄像头:

{audio:false,video:{facingMode:{exact:"environment"}}}
对于前摄像头:

{audio:false,video:{facingMode:"user"}}

我不明白,如果你可以在前后摄像头之间切换,那么听起来你可以选择前置摄像头,也可以选择后置摄像头,那么为什么浏览器重启后你不能选择后置摄像头呢?请澄清你的问题。读取数组的顺序似乎不相关。另请参见@jib,我的意思是当页面加载时,我希望手机使用其罕见的摄像头。当前,当页面加载前摄像头打开时,作为用户,我必须单击按钮选择稀有摄像头,这在我的场景中是不需要的。稀有cam是我的首选。你使用的是用户术语。编程问题也是如此。如果页面加载时打开相机的代码有问题,您需要向我们显示代码,以便我们发表评论。@jib,我理解您的意思。我的问题很简单,如何让chrome在默认情况下拾取后置摄像头?我已经共享了上面访问前置摄像头的代码。您共享的代码只返回可用设备的列表。您需要在某处调用
getUserMedia
。在没有看到代码的情况下,我不清楚你的问题出了什么问题。我不明白,如果你能够在前后摄像头之间切换,那么听起来你可以选择前摄像头,也可以选择后摄像头,那么为什么你不能在重新启动浏览器后选择后摄像头呢?请澄清你的问题。读取数组的顺序似乎不相关。另请参见@jib,我的意思是当页面加载时,我希望手机使用其罕见的摄像头。当前,当页面加载前摄像头打开时,作为用户,我必须单击按钮选择稀有摄像头,这在我的场景中是不需要的。稀有cam是我的首选。你使用的是用户术语。编程问题也是如此。如果页面加载时打开相机的代码有问题,您需要向我们显示代码,以便我们发表评论。@jib,我理解您的意思。我的问题很简单,如何让chrome在默认情况下拾取后置摄像头?我已经共享了上面访问前置摄像头的代码。您共享的代码只返回可用设备的列表。您需要在某处调用
getUserMedia
。没有看到代码,我就不清楚你的问题出了什么问题。