2

I found this code on GitHub. I tested it on android and it doesn't seem to work. The camera output isn't always displayed (out of the 4 times I tried it it only worked once.) and the "front" and "back" buttons don't work. Only the front facing camera is displayed, the "back" button doesn't dislpay the back camera.

Here's the code:

    <button onclick="stop();gum('user')">Front</button>

   <button onclick="stop();gum('environment')">Back</button>
    <div id="div"></div><br>
   <video id="video" height="320" autoplay></video>
 <script type="text/javascript">
var gum = mode => 
 navigator.mediaDevices.getUserMedia({video: {facingMode: {exact:     mode}}})
  .then(stream => (video.srcObject = stream))
  .catch(e => log(e));

var stop = () => video.srcObject && video.srcObject.getTracks().forEach(t     => t.stop());

var log = msg => div.innerHTML += msg + "<br>";
</script>

Thank you for your help.

  • Possible duplicate of [Enable rear camera with HTML5](http://stackoverflow.com/questions/18625007/enable-rear-camera-with-html5) – Mohammad Kermani Feb 20 '17 at 07:29
  • Sounds like you're using Chrome for Android, which still hasn't implemented it ([Firefox for Android](https://www.mozilla.org/en-US/firefox/android/) has!) - Use the [adapter.js](https://github.com/webrtc/adapter) polyfill for Chrome to fix it. – jib Feb 21 '17 at 02:43
  • Possible duplicate of [GetUserMedia - facingmode](http://stackoverflow.com/questions/32086122/getusermedia-facingmode) – jib Feb 21 '17 at 02:43

1 Answers1

0

change mode with "user" or "environment"

Ion Stefanache
  • 147
  • 1
  • 4