HTML5 getUserMedia() 媒体源

标签 html video webcam getusermedia

我用 html5 创建了一个流式网络摄像头。目前我可以通过我的网络摄像头拍照,但我想知道是否可以从列表中选择媒体流设备,例如我有两个网络摄像头,我想选择要激活的网络摄像头。我怎样才能用 html5 getUserMedia() 调用来做到这一点? 谢谢!

最佳答案

获取网络摄像头列表

<!DOCTYPE html>
      <head>
          <meta charset="utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Video Camera List</title>

          <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ></script>

          <style type="text/css" media="screen">
            video {
          border:1px solid gray;
        }
      </style>
  </head>
  <body>
    <script>
      if (!MediaStreamTrack) document.body.innerHTML = '<h1>Incompatible Browser Detected. Try <strong style="color:red;">Chrome Canary</strong> instead.</h1>';

      var videoSources = [];

      MediaStreamTrack.getSources(function(media_sources) {
        console.log(media_sources);
    //  alert('media_sources : '+media_sources);
        media_sources.forEach(function(media_source){
          if (media_source.kind === 'video') {
            videoSources.push(media_source);
          }
        });

        getMediaSource(videoSources);
      });

      var get_and_show_media = function(id) {
        var constraints = {};
        constraints.video = {
          optional: [{ sourceId: id}]
        };

        navigator.webkitGetUserMedia(constraints, function(stream) {
          console.log('webkitGetUserMedia');
          console.log(constraints);
          console.log(stream);

          var mediaElement = document.createElement('video');
          mediaElement.src = window.URL.createObjectURL(stream);
          document.body.appendChild(mediaElement);
          mediaElement.controls = true;
          mediaElement.play();

        }, function (e) 
        {
       //   alert('Hii');  
          document.body.appendChild(document.createElement('hr'));
          var strong = document.createElement('strong');
          strong.innerHTML = JSON.stringify(e);
          alert('strong.innerHTML : '+strong.innerHTML);
          document.body.appendChild(strong);
        });
      };

      var getMediaSource = function(media) {
        console.log(media);
        media.forEach(function(media_source) {
          if (!media_source) return;

          if (media_source.kind === 'video') 
          {
            // add buttons for each media item
            var button = $('<input/>', {id: media_source.id, value:media_source.id, type:'submit'});
            $("body").append(button);
            // show video on click
            $(document).on("click", "#"+media_source.id, function(e){
              console.log(e);
              console.log(media_source.id);
              get_and_show_media(media_source.id);
            });
          }
        });
      }
    </script>
  </body>
</html>

关于HTML5 getUserMedia() 媒体源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18061536/

相关文章:

css - HTML 表格大小被 IE 改变

php - Wordpress CSS 样式表不影响小部件输出

JQuery 移动按钮 : how to enable the blinking effect when overriding a theme?

javascript - 如何在页面上对齐视频、视频的高度和宽度以及进度条

android - Quickblox:与离线用户进行视频通话的策略是什么

video - 如何使用 ffmpeg 从网络摄像头访问压缩流

html - 申请提交时的 css 样式与 href 不同

带有 alpha channel 的 android native 视频播放

reactjs - React 网络摄像头组件在 chrome 扩展中不起作用

c++ - 使用 OpenCV 输出作为网络摄像头