代码之家  ›  专栏  ›  技术社区  ›  Sterling Butters

Javascript/HTML5:全屏视频源(网络摄像头或类似设备)

  •  2
  • Sterling Butters  · 技术社区  · 7 年前

    我可以在这里学习全屏API教程: https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API

    HTML格式:

      <video autoplay
          id = "video"
          src="SampleVideo.mp4">
          <script src="fullscreen.js"></script>
      </video>
    

    function toggleFullScreen() {
      if (!document.fullscreenElement) {
    
        var elem = document.getElementById("video");
        if (elem.requestFullscreen) {
          elem.requestFullscreen();
        } else if (elem.mozRequestFullScreen) {
          elem.mozRequestFullScreen();
        } else if (elem.webkitRequestFullscreen) {
          elem.webkitRequestFullscreen();
        } else if (elem.msRequestFullscreen) {
          elem.msRequestFullscreen();
        }
    
      } else {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        }
      }
    }
    
    document.addEventListener("keydown", function(e) {
      if (e.keyCode == 13) {
        console.log('enter')
        toggleFullScreen();
      }
    }, false);
    

    然后我按照这里的教程: https://www.html5rocks.com/en/tutorials/getusermedia/intro/

    const hdConstraints = {
      video: {width: {min: 1280}, height: {min: 720}}
    };
    
    navigator.mediaDevices.getUserMedia(hdConstraints).
      then((stream) => {video.srcObject = stream});
    
    const video = document.querySelector('video');
    
    const videoElement = document.querySelector('video');
    const audioSelect = document.querySelector('select#audioSource');
    const videoSelect = document.querySelector('select#videoSource');
    
    navigator.mediaDevices.enumerateDevices()
      .then(gotDevices).then(getStream).catch(handleError);
    
    audioSelect.onchange = getStream;
    videoSelect.onchange = getStream;
    
    function gotDevices(deviceInfos) {
      for (let i = 0; i !== deviceInfos.length; ++i) {
        const deviceInfo = deviceInfos[i];
        const option = document.createElement('option');
        option.value = deviceInfo.deviceId;
        if (deviceInfo.kind === 'audioinput') {
          option.text = deviceInfo.label ||
            'microphone ' + (audioSelect.length + 1);
          audioSelect.appendChild(option);
        } else if (deviceInfo.kind === 'videoinput') {
          option.text = deviceInfo.label || 'camera ' +
            (videoSelect.length + 1);
          videoSelect.appendChild(option);
        } else {
          console.log('Found another kind of device: ', deviceInfo);
        }
      }
    }
    
    function getStream() {
      if (window.stream) {
        window.stream.getTracks().forEach(function(track) {
          track.stop();
        });
      }
    
      const constraints = {
        audio: {
          deviceId: {exact: audioSelect.value}
        },
        video: {
          deviceId: {exact: videoSelect.value}
        }
      };
    
      navigator.mediaDevices.getUserMedia(constraints).
        then(gotStream).catch(handleError);
    }
    
    function gotStream(stream) {
      window.stream = stream; // make stream available to console
      videoElement.srcObject = stream;
    }
    
    function handleError(error) {
      console.error('Error: ', error);
    }
    

    我想知道为什么我不能把 src 在HTML中,将2个js文件合并在一起,并有一个全屏摄像头。全屏API的使用是否仅限于视频,即不限于视频源?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sterling Butters    7 年前

    见@kaido的答案: https://jsfiddle.net/kv6x2tf7/

    HTML格式:

    <select id="audioSource">
    </select>
    <select id="videoSource">
    </select>
    <video id="video" muted autoplay></video>
    

    function toggleFullScreen() {
      if (!document.fullscreenElement) {
    
        var elem = document.getElementById("video");
        if (elem.requestFullscreen) {
          elem.requestFullscreen();
        } else if (elem.mozRequestFullScreen) {
          elem.mozRequestFullScreen();
        } else if (elem.webkitRequestFullscreen) {
          elem.webkitRequestFullscreen();
        } else if (elem.msRequestFullscreen) {
          elem.msRequestFullscreen();
        }
    
      } else {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        }
      }
    }
    
    document.addEventListener("keydown", function(e) {
      if (e.keyCode == 13) {
        console.log('enter')
        toggleFullScreen();
      }
    }, false);
    
    const hdConstraints = {
      video: {width: {min: 1280}, height: {min: 720}}
    };
    
    navigator.mediaDevices.getUserMedia(hdConstraints).
      then((stream) => {video.srcObject = stream});
    
    const video = document.querySelector('video');
    
    const videoElement = document.querySelector('video');
    const audioSelect = document.querySelector('select#audioSource');
    const videoSelect = document.querySelector('select#videoSource');
    
    navigator.mediaDevices.enumerateDevices()
      .then(gotDevices).then(getStream).catch(handleError);
    
    audioSelect.onchange = getStream;
    videoSelect.onchange = getStream;
    
    function gotDevices(deviceInfos) {
      for (let i = 0; i !== deviceInfos.length; ++i) {
        const deviceInfo = deviceInfos[i];
        const option = document.createElement('option');
        option.value = deviceInfo.deviceId;
        if (deviceInfo.kind === 'audioinput') {
          option.text = deviceInfo.label ||
            'microphone ' + (audioSelect.length + 1);
          audioSelect.appendChild(option);
        } else if (deviceInfo.kind === 'videoinput') {
          option.text = deviceInfo.label || 'camera ' +
            (videoSelect.length + 1);
          videoSelect.appendChild(option);
        } else {
          console.log('Found another kind of device: ', deviceInfo);
        }
      }
    }
    
    function getStream() {
      if (window.stream) {
        window.stream.getTracks().forEach(function(track) {
          track.stop();
        });
      }
    
      const constraints = {
        audio: {
          deviceId: {exact: audioSelect.value}
        },
        video: {
          deviceId: {exact: videoSelect.value}
        }
      };
    
      navigator.mediaDevices.getUserMedia(constraints).
        then(gotStream).catch(handleError);
    }
    
    function gotStream(stream) {
      window.stream = stream; // make stream available to console
      videoElement.srcObject = stream;
    }
    
    function handleError(error) {
      console.error('Error: ', error);
    }
    

    或参见此处提到的样式表更改:

    Changing Style Sheet javascript