哪个相机会在移动设备中打开getUserMedia API?前面还是后面?

Nis*_*sin 12 javascript html5 getusermedia

使用getUserMedia API访问桌面摄像头时,它会打开网络摄像头.当然它有助于视频通信.但是当它在移动设备中使用时调用哪个摄像头.前凸轮或后凸轮?.是否需要选择代码相机?

Ste*_*ter 16

有一个解决方案,用户可以选择其中一个摄像头.

使用HTML5启用后置摄像头

通过sourceInfo.facing以下代码进行评估,您可以选择一个摄像头("用户"或"环境")(适用于当前的chrome> = 30):https://simpl.info/getusermedia/sources/

'use strict';

var videoElement = document.querySelector('video');
var audioSelect = document.querySelector('select#audioSource');
var videoSelect = document.querySelector('select#videoSource');

navigator.getUserMedia = navigator.getUserMedia ||
  navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

function gotSources(sourceInfos) {
  for (var i = 0; i !== sourceInfos.length; ++i) {
    var sourceInfo = sourceInfos[i];
    var option = document.createElement('option');
    option.value = sourceInfo.id;
    if (sourceInfo.kind === 'audio') {
      option.text = sourceInfo.label || 'microphone ' + (audioSelect.length + 1);
      audioSelect.appendChild(option);
    } else if (sourceInfo.kind === 'video') {
      option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
      videoSelect.appendChild(option);
    } else {
      console.log('Some other kind of source: ', sourceInfo);
    }
  }
}

if (typeof MediaStreamTrack === 'undefined'){
  alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
} else {
  MediaStreamTrack.getSources(gotSources);
}


function successCallback(stream) {
  window.stream = stream; // make stream available to console
  videoElement.src = window.URL.createObjectURL(stream);
  videoElement.play();
}

function errorCallback(error){
  console.log('navigator.getUserMedia error: ', error);
}

function start(){
  if (!!window.stream) {
    videoElement.src = null;
    window.stream.stop();
  }
  var audioSource = audioSelect.value;
  var videoSource = videoSelect.value;
  var constraints = {
    audio: {
      optional: [{sourceId: audioSource}]
    },
    video: {
      optional: [{sourceId: videoSource}]
    }
  };
  navigator.getUserMedia(constraints, successCallback, errorCallback);
}

audioSelect.onchange = start;
videoSelect.onchange = start;

start();
Run Code Online (Sandbox Code Playgroud)


Mik*_*ren 6

不幸的是,你不能(但是?)通过代码选择它.

  • Mozilla Firefox beta:当用户接受共享摄像头时,他/她还会选择要分享的摄像头.

  • Chrome测试版:我只能使用面部相机.可配置,但我不知道如何......

编辑:在Chrome中,可以通过编程方式选择背面摄像头.请参阅此主题中Probot的下一个答案.