HTML userMedia facingMode: environmentdoesn't work on android phone
Your constraints are not set correctly.
facingMode
is a member of the video
constraint, so it should be
const constraints = {
video: {
facingMode: {
exact: "environment"
}
}
};
Live Fiddle to be ran from a device with a back camera.
navigator.mediaDevices.getUserMedia Video quality and facing mode constraints
navigator.mediaDevices.getUserMedia({ audio: true, video: getConstraints(1280, 720) });
function getConstraints(videowidth, videoheight) {
constraints = {
facingMode: { exact: "environment" },
width: { min: videowidth, ideal: videowidth, max: videowidth },
height: { min: videoheight, ideal: videoheight, max: videoheight },
frameRate: { min: 5, max: 8 }
};
return constraints;
}
How to switch between front camera and rear camera in javascript?
Something like this :
function handleVideo(cameraFacing) {
const constraints = {
video: {
facingMode: {
exact: cameraFacing
}
}
}
return constraints
};
function turnVideo(constraints) {
let video;
navigator.mediaDevices.getUserMedia(constraints)
.then((stream) => {
video = document.createElement("video")
video.srcObject = stream
video.play()
video.onloadeddata = () => {
ctx.height = video.videoHeight
}
})
}
document.querySelector(".frontCamera").addEventListener("click", () => {
turnVideo(handleVideo("user"));
})
document.querySelector(".backCamera").addEventListener("click", () => {
turnVideo(handleVideo("enviroment"));
})
<div class="frontCamera">front</div>
<div class="backCamera">back</div>
Using mediaDevices.getUserMedia to access front facing camera on a laptop
The facingMode
constraint is the right answer according to the spec, and works in Firefox. Chrome just hasn't implemented it yet. However, you can use the adapter.js polyfill to get it.
Try it out here.
Related Topics
Does JavaScript Process Using an Elastic Racetrack Algorithm
JSON Stringify Changes Time of Date Because of Utc
Switch Case Not Showing Correct Results
How to Create a Simple Http Proxy in Node.Js
Differencebetween Document and Document in JavaScript
Ajax Post and Plus Sign ( + ) -- How to Encode
What Does the at Symbol (@) Do in Es6 JavaScript? (Ecmascript 2015)
How to Create a Custom Error in JavaScript
Clearing Localstorage in JavaScript
Event Listener for When Element Becomes Visible
Wrapping a Set of Dom Elements Using JavaScript
Editing in the Chrome Debugger
Call a Vue.Js Component Method from Outside the Component
How to Ignore User's Time Zone and Force Date() Use Specific Time Zone
Is There a Downside to Using Es6 Template Literals Syntax Without a Templated Expression