LoginSignup
11
3

More than 5 years have passed since last update.

webRTCがipadなら繋がるのにiphoneでは繋がらない時の対処法

Posted at

JavaScriptでwebRTCのgetUserMediaのコードをコピペしてipadで正常に挙動するのにiphoneではアクセス許可しているのにも関わらず、画面が真っ黒になってgetUserMediaが渡っていない現象が出て暫く躓いたので対処法のメモ。

$(function() {
    navigator.mediaDevices = navigator.mediaDevices || ((navigator.mozGetUserMedia || navigator.webkitGetUserMedia) ? {
   getUserMedia: function(c) {
     return new Promise(function(y, n) {
       (navigator.mozGetUserMedia ||
        navigator.webkitGetUserMedia).call(navigator, c, y, n);
     });
    }
    } : null);
    if (!navigator.mediaDevices) {
      alert("getUserMedia() not supported.");
      return;
    } else {
        console.log("success")
    }

    var constraints = { audio: false, video: { 
                        facingMode: 'environment' 
                            }
                    };
    navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
      video = document.getElementById('camera');
      video.srcObject = stream;
    }).catch(function(err) {
      console.log(err.name + ": " + err.message);
    });
   <video id="camera" autoplay playsinline="true"></video>

autoplayの後にplaysinline="true"を付け加えるだけでOK。
playsinline属性がないとiPhoneでは動かないらしい。

<参考サイト>
https://qiita.com/tomoyukilabs/items/cb9dd1d3e7eb0cc7f58a

11
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
3