Skip to main content
JavaScript

Stream video with getUserMedia()

By April 13, 2020July 19th, 2022No Comments

Request the user to start their webcam stream

  1. Using the getUserMedia() API which allows you to access a user’s camera and mic without any plugins
  2. Streams have inputs (video) and outputs (video)
  3. The if-conditional checks if the getUserMedia is supported
  4. Syntax: navigator.mediaDevices.getUserMedia(constraints).then(successCallback).catch(errorCallback)
  5. The getUserMedia() method takes one argument called the MediaStreamConstraints object which allows you to specify the constrains/specifications of the media . In this case video is set to true
  6. Media.getUserMedia() returns then method which returns a promise which will take 2 parameter:
  • a callback function which resolves to a media stream object, in this case video
  • failure function for a failure
  1. If there is is an error, or the user refuses to give permission to access the webcam then ‘error’ is logged to the console

Leave a Reply