-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestMedia-2.0.0.min.js
1 lines (1 loc) · 5.17 KB
/
requestMedia-2.0.0.min.js
1
var requestMedia=function(e){if(!navigator.mediaDevices.getUserMedia||null==window.MediaRecorder)throw"Not supported on your browser, use the latest version of Firefox or Chrome'";for(var o in this.defaults={preview:null,output:null,onGetPermission:function(){},onForgetPermission:function(){},onError:function(e){console.log(e.name+": "+e.message)},onStartRecording:function(){},onStopRecording:function(){},onPictureTaken:function(){},onDownload:function(){},audio:!0,video:{width:{min:640,ideal:1280,max:1080},height:{min:480,ideal:720,max:1980},frameRate:{min:24,ideal:60,max:120},facingMode:"user"}},this.defaults)this[o]=(void 0!==e[o]?e:this.defaults)[o];this.dataArray=[],this.stream=null,this.recorder=null,this.blob=null,this.urlobj=null,this.canvas=null,this.fileName=null,this.fileType="video/webm",this._getOptionRecorder=function(){if("function"==typeof MediaRecorder.isTypeSupported){var e={mimeType:null};return MediaRecorder.isTypeSupported("video/webm;codecs=vp9")?e.mimeType="video/webm;codecs=vp9":MediaRecorder.isTypeSupported("video/webm;codecs=h264")?e.mimeType="video/webm;codecs=h264":MediaRecorder.isTypeSupported("video/webm")?e.mimeType="video/webm":MediaRecorder.isTypeSupported("video/mp4")&&(e.mimeType="video/mp4"),console.log("Using "+e.mimeType),e}}};requestMedia.prototype.requestPermission=function(){var o=this;navigator.mediaDevices.getUserMedia({video:o.video,audio:o.audio}).then(function(e){o.stream=e,o.stream.getTracks().forEach(function(o){"audio"==o.kind&&(o.onended=function(e){console.log("audio track.onended Audio track.readyState="+o.readyState+", track.muted="+o.muted)}),"video"==o.kind&&(o.onended=function(e){console.log("video track.onended Audio track.readyState="+o.readyState+", track.muted="+o.muted)})}),!o.preview||"object"!=typeof o.preview||"AUDIO"!==o.preview.nodeName&&"VIDEO"!==o.preview.nodeName||(o.preview.srcObject=o.stream,o.preview.play())}).catch(function(e){o.onError&&"function"==typeof this.onError?o.onError(e):console.log("Request access error: "+e)})},requestMedia.prototype.forgetPermission=function(){null!=this.stream&&(this.stream.getTracks().forEach(function(e){e.stop()}),this.onForgetPermission&&"function"==typeof this.onForgetPermission&&this.onForgetPermission())},requestMedia.prototype.startRecording=function(){var o=this;if(null==o.stream)throw"Could not get local stream from mic/camera";o.dataArray=[],o.fileType=null,o.blob=null,o.recorder=null,o.fileName=null,console.log("Starting...");var e=o._getOptionRecorder();e?(o.fileType=e.mimeType,o.recorder=new MediaRecorder(o.stream,e)):o.recorder=new MediaRecorder(o.stream),o.recorder.ondataavailable=function(e){console.log("mediaRecorder.ondataavailable, e.data.size="+e.data.size),e.data&&0<e.data.size&&o.dataArray.push(e.data)},o.onError&&"function"==typeof o.onError?o.recorder.onerror=o.onError:o.recorder.onerror=function(e){console.log("Recording error: "+e)},o.onStartRecording&&"function"==typeof o.onStartRecording?o.recorder.onstart=o.onStartRecording():o.recorder.onstart=function(){console.log("Start recording, that.recorder.state = "+o.recorder.state)},o.recorder.onstop=function(){console.log("Stop recording, that.recorder.state = "+o.recorder.state),o.blob=new Blob(o.dataArray,{type:o.recorder.mimeType}),!o.output||"object"!=typeof o.output||"AUDIO"!==o.output.nodeName&&"VIDEO"!==o.output.nodeName||(o.output.src=URL.createObjectURL(o.blob),o.output.controls=!0),"video/mp4"===o.fileType?o.fileName=["file_",(new Date+"").slice(4,28),".mp4"].join(""):o.fileName=["file_",(new Date+"").slice(4,28),".webm"].join("")},o.recorder.start(100)},requestMedia.prototype.stopRecording=function(){var e=this;e.recorder.stop(),e.onStopRecording&&"function"==typeof e.onStopRecording&&setTimeout(e.onStopRecording(),500)},requestMedia.prototype.download=function(){var e=this;e.onDownload&&"function"==typeof e.onDownload&&e.onDownload(),a=document.createElement("a"),a.style="display: none",a.download=e.fileName,e.urlobj?a.href=e.urlobj:a.href=URL.createObjectURL(e.blob),a.textContent=e.fileName,document.body.appendChild(a),a.click(),setTimeout(function(){document.body.removeChild(a),URL.revokeObjectURL(a.href)},500)},requestMedia.prototype.startCanvas=function(e){var o=this;if(null==o.stream)throw"Could not get local stream from mic/camera";var t=e,r=0,e=null;o.canvas&&(r=o.preview.videoHeight/(o.preview.videoWidth/t),isNaN(r)&&(r=9*t/12),o.canvas.setAttribute("width",t),o.canvas.setAttribute("height",r)),(e=o.canvas.getContext("2d")).fillStyle="#AAA",e.fillRect(0,0,o.canvas.width,o.canvas.height);e=o.canvas.toDataURL("image/png");o.output.setAttribute("src",e)},requestMedia.prototype.takePicture=function(){var e=this;if(null==e.stream)throw"Could not get local stream from mic/camera";var o=e.canvas.getContext("2d");if(e.canvas.width&&e.canvas.height){width=e.canvas.width,height=e.canvas.height,o.drawImage(e.preview,0,0,width,height);var t=e.canvas.toDataURL("image/png");e.output.setAttribute("src",t);for(var o=(e.urlobj=t).split(";base64,"),t=o[0].split(":")[1],r=window.atob(o[1]),i=r.length,n=new Uint8Array(i),a=0;a<i;++a)n[a]=r.charCodeAt(a);e.blob=new Blob([n],{type:t}),e.onPictureTaken&&"function"==typeof e.onPictureTaken&&setTimeout(e.onPictureTaken(),50)}else console.log("erro"),console.log(e.canvas)};