2015/09/22

HTML5 存取相機與聲音

<!DOCTYPE html>
<html lang="zh-tw">
 <head>
  <meta charset="utf8">
  <script type="text/javascript">
   navigator.getUserMedia = navigator.getUserMedia ||
          navigator.webkitGetUserMedia ||
          navigator.mozGetUserMedia ||
          navigator.msGetUserMedia;

   if(navigator.getUserMedia){
    navigator.getUserMedia({video : true, audio : true},
     function (stream) {
      var video = document.querySelector('video');
      video.src = window.URL.createObjectURL(stream);
      video.onloadedmetadata = function(e){
       video.play();
      };
     },
     function (err) {
      console.log(err);
     }
    );
   }else
    alert("無法使用相機");
  </script>
  <title>Hi</title>
 </head>
 <body>
  <video autoplay></video>
 </body>
</html>