Skip to content

Latest commit

 

History

History
48 lines (35 loc) · 1.48 KB

Screen Capture API.md

File metadata and controls

48 lines (35 loc) · 1.48 KB

Screen Capture API

Screen Capture API 对现有的媒体捕获和流 API 进行了补充,让用户选择一个屏幕或屏幕的一部分(如一个窗口)作为媒体流进行捕获。然后,该流可以被记录或通过网络与他人共享。

const video = document.getElementById('screen-video')
const hasSupport = () => Boolean('mediaDevices' in navigator)

async function onCapture() {
  video.srcObject = await navigator.mediaDevices.getDisplayMedia({
    video: {
      aspectRatio: 16 / 9
    },
    audio: {
      echoCancellation: true,
      sampleRate: 44100
    }
  })

  const track = video.srcObject.getVideoTracks()[0]

  console.info(JSON.stringify(track.getSettings(), null, 2))
  console.info(JSON.stringify(track.getConstraints(), null, 2))
}

function onStop() {
  const video = document.getElementById('screen-video')

  video.srcObject.getTracks().forEach((stream) => stream.stop())

  video.srcObject = null
}

startCapture.addEventListener('click', () => {
  onCapture()
})

stopCapture.addEventListener('click', () => {
  onStop()
})

示例地址

更多资料