使用getUserMedia API在桌面上訪問攝像頭時,它將打開網絡攝像頭。當然,它有助於視頻通信,但是在移動設備中使用哪個攝像頭時會被調用。前方攝像頭或後方攝像頭需要編碼選擇相機?哪個攝像頭將在移動設備中打開getUserMedia API?前面還是後面?
12
A
回答
6
不幸的是,你不能(但?)通過代碼選擇它。
Mozilla Firefox瀏覽器測試版:當用戶接受共享相機,他/她也 選擇要分享的相機。
Chrome beta:我只能使用臉部相機。可能是 配置的,但我不知道怎麼...
編輯:在Chrome有可能選擇編程面對鏡頭後面。在此線程中查看Probot的下一個答案。
16
有一個解決方案,用戶可以選擇其中一個攝像頭。
通過從下面的代碼評估sourceInfo.facing
,可以選擇一個相機( '用戶' 或 '環境')(其在當前鉻工作> = 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();
+0
此代碼不工作從我,我改變視頻可選的ID,但不工作...... –
2
答案是肯定的,對於Android作爲默認相機,前面(用戶) caerma已啓動。因此,我建議此腳本在前置攝像頭和後置攝像頭之間進行選擇
//----------------------------------------------------------------------
// Here we list all media devices, in order to choose between
// the front and the back camera.
// videoDevices[0] : Front Camera
// videoDevices[1] : Back Camera
// I used an array to save the devices ID
// which i get using devices.forEach()
// Then 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));
相關問題
- 1. 訪問設備攝像頭與getUserMedia
- 2. 訪問攝像頭源並加載到移動設備頁面
- 3. 在表面視圖中打開前置攝像頭
- 4. 同時打開android前置攝像頭和後置攝像頭
- 5. getUserMedia - 如何檢測設備實際上是否有攝像頭
- 6. 只有在射擊意圖打開攝像頭後纔打開設備攝像頭
- 7. 網絡攝像頭getUserMedia API - AngularJS錯誤?
- 8. getUserMedia()音頻塊攝像頭
- 9. 檢查哪個攝像頭是開放的正面或背面Android
- 10. 訪問多個攝像頭的JavaScript getusermedia
- 11. Android/Java:檢測設備是否有背面攝像頭
- 12. 如何檢測Windows移動設備是否有攝像頭? c#
- 13. 停止getUserMedia的網絡攝像頭流沒有頁面刷新
- 14. Android攝像頭API 2自動對焦前置攝像頭
- 15. 是否可以同時打開並預覽背面和前置攝像頭?
- 16. Unity3d - 移動攝像頭從字面上將FPS削減一半?
- 17. 識別頁面是用於PC還是移動設備?
- 18. Android前置攝像頭API
- 19. Selenium ChromeDriver - 記住「始終允許」設置getUserMedia攝像頭訪問
- 20. 檢查網絡攝像頭攝像頭設備GNOME庫
- 21. 打開安卓攝像頭
- 22. 如何使用移動設備(Android設備,iPhone)jQuery的攝像頭?
- 23. 在XNA中移動攝像頭,C#
- 24. 我如何檢測庫圖像是來自前置攝像頭還是後置攝像頭
- 25. 2D移動攝像頭(LWJGL)
- 26. 在移動設備上打開圖(iOS)
- 27. 在後置攝像頭和前置攝像頭之間切換
- 28. 在UILabels後面設置背景攝像頭
- 29. 在UC android瀏覽器中使用攝像頭設備(WebRTC API)
- 30. HTML5-如何打開多個攝像頭
在手機默認是前面? –
在Chrome beta上它是。在Firefox測試版中,默認回來,但正如我所提到的,用戶可以在接受共享攝像頭(在Firefox上)時選擇此選項。 –
如何通過代碼選擇..沒辦法? –