GetUserMedia - facingMode not recognized despite adapter.js

余生颓废 提交于 2020-01-05 09:23:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!