菜鸟教程小白 发表于 2022-12-12 14:38:25

javascript - Webrtc后置摄像头在IOS 11中不起作用


                                            <p><p>下面是我的代码,可以在带有前置摄像头的 android 和 ios 11 中正常工作,而当我使用后置摄像头时会出错</p>

<p><strong>以下作品</strong></p>

<pre><code>video: { width: 400, height: 200, facingMode: &#34;user&#34;}
</code></pre>

<p><strong>以下无效</strong></p>

<pre><code>video: { width: 400, height: 200, facingMode: &#34;environment&#34;}
</code></pre>

<p><strong>完整代码</strong></p>

<p>**</p>

<pre><code>var constraints = window.constraints = {
audio: false,
video: { width: 400, height: 200, facingMode: &#34;environment&#34;}
};
function handleSuccess(stream) {
var videoTracks = stream.getVideoTracks();
console.log(&#39;Got stream with constraints:&#39;, constraints);
console.log(&#39;Using video device: &#39; + videoTracks.label);
stream.oninactive = function() {
    console.log(&#39;Stream inactive&#39;);
};
window.stream = stream; // make variable available to browser console
video.srcObject = stream;
}
function handleError(error) {
if (error.name === &#39;ConstraintNotSatisfiedError&#39;) {
    errorMsg(&#39;The resolution &#39; + constraints.video.width.exact + &#39;x&#39; +
      constraints.video.width.exact + &#39; px is not supported by your device.&#39;);
} else if (error.name === &#39;PermissionDeniedError&#39;) {
    errorMsg(&#39;Permissions have not been granted to use your camera and &#39; +
      &#39;microphone, you need to allow the page access to your devices in &#39; +
      &#39;order for the demo to work.&#39;);
}
errorMsg(&#39;getUserMedia error: &#39; + error.name, error);
}
function errorMsg(msg, error) {
errorElement.innerHTML += &#39;&lt;p&gt;&#39; + msg + &#39;&lt;/p&gt;&#39;;
if (typeof error !== &#39;undefined&#39;) {
    console.error(error);
}
}
navigator.mediaDevices.getUserMedia(constraints).
    then(handleSuccess).catch(handleError);
</code></pre>

<p>**</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>目前苹果iOS11下的<code>getUserMedia</code><strong>不支持<code>width</code>和<code>height</code>约束。</strong> </p >

<p>使用后置摄像头的唯一方法是通过以下约束:</p>

<pre><code>var constraints = window.constraints = {
   audio: false,
   video: { facingMode: &#39;environment&#39; }
};
</code></pre>

<p>我为此在 Apple 中打开了一个错误,但它已作为副本关闭。
请密切关注 Apple 关于此问题的更新。</p></p>
                                   
                                                <p style="font-size: 20px;">关于javascript - Webrtc后置摄像头在IOS 11中不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49206667/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49206667/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - Webrtc后置摄像头在IOS 11中不起作用