navigator.getUserMedia not working on Android / Chrome

前端 未结 2 1655
南旧
南旧 2021-02-07 19:40

do you have any idea of why the following code doesn\'t work on Android/Chrome?

It works well on Desktop/Chrome.

相关标签:
2条回答
  • 2021-02-07 20:05

    I've had the same problem. Mobile browser even haven't asked about permissions. And the reason was SSL! You have to use secure connection

    Check "Secure context required" section here

    0 讨论(0)
  • 2021-02-07 20:15

    According to MDN, navigator.getUserMedia() is deprecated and isn't supported on Android/Chrome and some newer browser versions. Use navigator.mediaDevices.getUserMedia() instead. You can check browser compatibility below.

    MDN Navigator.getUserMedia browser check

    Here's a partial example I've used to access the camera for video streaming in a past project. The browser should ask the user for access on the device.

    if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
      navigator.mediaDevices.getUserMedia({ audio: false, video:  cameraOrientation })
        .then(function(stream) {
          if ("srcObject" in video) {
              video.srcObject = stream;
            } else {
              video.src = window.URL.createObjectURL(stream);
            }
            video.onloadedmetadata = function(e) {
              video.play();
            };
        });
    };
    
    0 讨论(0)
提交回复
热议问题