【问题标题】:GetUserMedia - facingMode not recognized despite adapter.jsGetUserMedia - facesMode 无法识别,尽管 adapter.js
【发布时间】:2017-04-20 09:18:39
【问题描述】:

我想在 Ionic 应用程序中获取后置摄像头的视频流。 为此,我使用了可在前置摄像头上正常工作的 getUserMedia。

当我将面对模式更改为“环境”时,我收到了这个错误:

Unknown constraint named facingMode rejected ConstraintNotSatisfiedError

在我的 Ionic 应用程序中,我已经安装了 npm 包“webrtc-adapter”。

这是我从后置摄像头获取流的代码:

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);

我认为这是一个兼容性问题。任何人都可以帮助我吗?

谢谢。

【问题讨论】:

    标签: javascript ionic-framework webrtc getusermedia mediastream


    【解决方案1】:

    您使用的是过时的非标准约束语法。 adapter.js 填充了规范,所以要从中受益,你必须 follow the spec。 例如。而不是:

    {audio: true, video: {mandatory: {facingMode: 'environment'}}};
    

    使用

    {audio: true, video: {facingMode: {exact: 'environment'}}};
    

    我已经有an answer with a working example of this。它应该适用于 Chrome。不确定这是否适用于离子。如果它不起作用,请告诉我。

    【讨论】:

    • 感谢您的回答,但是当我使用此语法时,我没有错误,但相机仍处于“用户”模式
    • @DamienLD 确保您已更新到适配器的latest version。另外,这是在什么设备上,what cameras 有吗?
    • 我有最新版本的适配器 (3.3.3),我在两台设备上运行代码:Huawei Honor 和 Huawei P9 lite。我每个都有两个摄像头,但 d.label 什么也没返回,有问题吗?
    • 经过大量研究,我发现我错过了使用mediaDevices.enumerateDevices()的权限。你知道我应该添加哪个权限吗?
    • 最后我决定不使用faceMode。就我而言,我创建了一个按钮来在设备的每个摄像头之间切换。为此,我使用mediaDevices.enumerateDevices() 来获取所有可用的摄像头。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-02-01
    • 2013-09-06
    • 1970-01-01
    • 2020-01-14
    • 2014-08-31
    • 2019-06-10
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多