【问题标题】:webrtc using front camera by default in chrome 54webrtc 在 chrome 54 中默认使用前置摄像头
【发布时间】:2021-12-10 09:38:48
【问题描述】:

伙计们,我一直在尝试使用 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:我尝试在其他代码中使用 facesMode: "environment",但这里有什么范围吗?

【问题讨论】:

  • 我不明白,如果你可以在前后摄像头之间切换,那听起来你可以选择前置摄像头,你可以选择后置摄像头,那为什么重启浏览器后你不能选择后置摄像头吗?请澄清你的问题。您读取数组的顺序似乎无关紧要。另见stackoverflow.com/q/40779564/918910
  • @jib,我的意思是当页面加载时,我希望手机使用其罕见的摄像头。目前,当页面加载时,前置摄像头打开,作为用户,我必须单击一个按钮来选择罕见的摄像头,这在我的场景中不需要。罕见的凸轮是我的首选。
  • 您使用的是用户条款。 SO 用于编程问题。如果您在页面加载时打开相机的代码有问题,您需要将代码展示给我们,以便我们评论。
  • @jib,我明白你在说什么。我的问题很简单,如何让chrome默认拿起后置摄像头?我已经分享了上面访问前置摄像头的代码。
  • 您共享的代码仅返回可用设备的列表。您需要在某处致电getUserMedia。在没有看到该代码的情况下,我不清楚您的问题出了什么问题。

标签: javascript android google-chrome camera webrtc


【解决方案1】:

如何使用约束

对于后置摄像头:

{audio:false,video:{facingMode:{exact:"environment"}}}

对于前置摄像头:

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

【讨论】:

    【解决方案2】:

    检查 deviceInfo 标签中的面并设置默认值。

    if(deviceInfo.label.indexOf('back') > 0) { option.selected = true; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2020-07-23
      相关资源
      最近更新 更多