使用getUserMedia API访问桌面摄像头时,会打开网络摄像头。当然这对视频通信有帮助。但是在移动设备中使用时会调用哪个摄像头。前置摄像头或后置摄像头?是否需要选择代码相机?
问问题
22685 次
3 回答
16
有一个解决方案,用户可以选择其中一个相机。
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();
于 2014-01-07T12:30:02.197 回答
6
不幸的是,您不能(还?)通过代码选择它。
Mozilla Firefox beta:当用户接受共享相机时,他/她也选择了要共享的相机。
Chrome beta:我只能使用面部摄像头。可以配置,但我不知道怎么配置……</p>
编辑:在 chrome 中,可以以编程方式选择后置摄像头。请参阅此线程中 Probot 的下一个答案。
于 2013-09-02T06:24:34.620 回答
4
答案是肯定的,对于 Android,默认摄像头是前置(<=> 用户)摄像头。所以我开发了下面的脚本来在前置摄像头和后置摄像头之间进行选择
//----------------------------------------------------------------------
// Here we list all media devices, in order to choose between
// the front camera and the rear camera.
// videoDevices[0] : Front Camera
// videoDevices[1] : Back Camera
// I used an array to save the devices ID
// which I get with using devices.forEach()
// Then I set the video resolution.
//----------------------------------------------------------------------
navigator.mediaDevices.enumerateDevices()
.then(devices => {
var videoDevices = [0,0];
var videoDeviceIndex = 0;
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
if (device.kind == "videoinput") {
videoDevices[videoDeviceIndex++] = device.deviceId;
}
});
var constraints = {width: { min: 1024, ideal: 1280, max: 1920 },
height: { min: 776, ideal: 720, max: 1080 },
deviceId: { exact: videoDevices[1] }
};
return navigator.mediaDevices.getUserMedia({ video: constraints });
})
.then(stream => {
if (window.webkitURL) {
video.src = window.webkitURL.createObjectURL(stream);
localMediaStream = stream;
} else if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream;
} else if (video.srcObject !== undefined) {
video.srcObject = stream;
} else {
video.src = stream;
}})
.catch(e => console.error(e));
于 2017-03-20T09:28:31.107 回答