SHARE
TWEET

Untitled

a guest Aug 20th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (navigator.mediaDevices === undefined) {
  2.         navigator.mediaDevices = {};
  3.         navigator.mediaDevices.getUserMedia = function(constraintObj) {
  4.             let getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  5.             if (!getUserMedia) {
  6.                 return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  7.             }
  8.             return new Promise(function(resolve, reject) {
  9.                 getUserMedia.call(navigator, constraintObj, resolve, reject);
  10.             });
  11.         }
  12.     }
  13.     else{
  14.         navigator.mediaDevices.enumerateDevices()
  15.         .then(devices => {
  16.             devices.forEach(device=>{
  17.                 console.log(device.kind.toUpperCase(), device.label);
  18.             })
  19.         })
  20.         .catch(err=>{
  21.             console.log(err.name, err.message);
  22.         })
  23.     }
  24.     navigator.mediaDevices.getUserMedia(constraintObj)
  25.     .then(function(mediaStreamObj) {
  26.         //connect the media stream to the first video element
  27.         let video = document.querySelector('video');
  28.         if ("srcObject" in video) {
  29.             video.srcObject = mediaStreamObj;
  30.         } else {
  31.             video.src = window.URL.createObjectURL(mediaStreamObj);
  32.         }
  33.  
  34.         video.onloadedmetadata = function(ev) {
  35.             //show in the video element what is being captured by the webcam
  36.             video.play();
  37.         };
  38.  
  39.         let start = document.getElementById('btnStart');
  40.         let stop = document.getElementById('btnStop');
  41.         let vidSave = document.getElementById('vid2');
  42.         let mediaRecorder = new MediaRecorder(mediaStreamObj);
  43.         let chunks = [];
  44.  
  45.         start.addEventListener('click', (ev)=>{
  46.             mediaRecorder.start();
  47.             console.log(mediaRecorder.state);
  48.         })
  49.         stop.addEventListener('click', (ev)=>{
  50.             mediaRecorder.stop();
  51.             console.log(mediaRecorder.state);
  52.         });
  53.         mediaRecorder.ondataavailable = function(ev) {
  54.             chunks.push(ev.data);
  55.         }
  56.         mediaRecorder.onstop = (ev)=>{
  57.             let blob = new Blob(chunks, { 'type' : 'video/mp4;' });
  58.             chunks = [];
  59.             let videoURL = window.URL.createObjectURL(blob);
  60.             vidSave.src = videoURL;
  61.         }
  62.     })
  63.     .catch(function(err) {
  64.         console.log(err.name, err.message);
  65.     });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top