问题
I want to get the video stream of my rear camera in an Ionic Application. For this, I use getUserMedia that work correctly for the front camera.
When I change the facing mode to 'environment' I got this error :
Unknown constraint named facingMode rejected
ConstraintNotSatisfiedError
In my Ionic application I have already installed the npm package "webrtc-adapter".
Here is my code to get the stream from the rear camera :
this.constraints = { audio: true, video: {mandatory: { facingMode: 'environment'}}};
cordova.plugins.diagnostic.requestRuntimePermission( (status) => {
if (cordova.plugins.diagnostic.permissionStatus.GRANTED){
navigator.getUserMedia(this.constraints, (stream) => {
let video = <HTMLVideoElement>document.getElementById('localVideo');
video.srcObject = stream;
}, function(err){
console.log("Error get stream: ", err.name);
});
}
}, (error) => {
console.error("Error during runtime permission :", error);
}, cordova.plugins.diagnostic.permission.CAMERA);
I think that is a compatibility issue. Anyone can help me ?
Thank you.
回答1:
You're using an outdated non-standard constraints syntax. adapter.js polyfills the spec, so to benefit from it you have to follow the spec. E.g. instead of:
{audio: true, video: {mandatory: {facingMode: 'environment'}}};
use
{audio: true, video: {facingMode: {exact: 'environment'}}};
I already have an answer with a working example of this. It should work with Chrome. Not sure if this will work with ionic or not. Let me know if it doesn't work.
来源:https://stackoverflow.com/questions/43515300/getusermedia-facingmode-not-recognized-despite-adapter-js