-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestMedia.min.js
1 lines (1 loc) · 6.31 KB
/
requestMedia.min.js
1
var requestMedia=function(e){for(var t in this.defaults={feed_in:null,feed_out:null,request_video:!0,request_audio:!0,name:["file_",(new Date+"").slice(4,28),".mp4"].join(""),required_audio:!0,required_video:!0,file_type:"video/mp4",onGetPermission:function(){},onForgetPermission:function(){},onDeniedPermission:function(e){console.log(e.name+": "+e.message)},onStartRecording:function(){},onStopRecording:function(){},onPictureTaken:function(){},onDownload:function(){}},this.defaults)this[t]=(void 0!==e[t]?e:this.defaults)[t];this.data_array=[],this.stream=null,this.recorder=null,this.blob=null,this.browser=navigator.userAgent,this.canvas=null};requestMedia.prototype.getFeed_in=function(){return this.feed_in},requestMedia.prototype.getFeed_out=function(){return this.feed_out},requestMedia.prototype.getRequest_video=function(){return this.request_video},requestMedia.prototype.getRequest_audio=function(){return this.request_audio},requestMedia.prototype.getName=function(){return this.name},requestMedia.prototype.getFile_type=function(){return this.file_type},requestMedia.prototype.getOnGetPermission=function(){return this.onGetPermission},requestMedia.prototype.getOnForgetPermission=function(){return this.onForgetPermission},requestMedia.prototype.getOnDeniedPermission=function(){return this.onDeniedPermission},requestMedia.prototype.getOnStartRecording=function(){return this.onStartRecording},requestMedia.prototype.getOnStopRecording=function(){return this.onStopRecording},requestMedia.prototype.getOnPictureTaken=function(){return this.onPictureTaken},requestMedia.prototype.getOnDownload=function(){return this.onDownload},requestMedia.prototype.getData_array=function(){return this.data_array},requestMedia.prototype.getStream=function(){return this.stream},requestMedia.prototype.getRecorder=function(){return this.recorder},requestMedia.prototype.getBlob=function(){return this.blob},requestMedia.prototype.getRequired_audio=function(){return this.required_audio},requestMedia.prototype.getRequired_video=function(){return this.required_video},requestMedia.prototype.getCanvas=function(){return this.canvas},requestMedia.prototype.setCanvas=function(e){this.canvas=e},requestMedia.prototype.forgetPermission=function(){if(null!=this.stream){for(var e=0;e<this.stream.getTracks().length;e++)this.stream.getTracks()[e].stop();this.onForgetPermission&&"function"==typeof this.onForgetPermission&&this.onForgetPermission()}},requestMedia.prototype.requestPermission=function(){var t=this;navigator.mediaDevices.getUserMedia({video:t.request_video,audio:t.request_audio}).then(function(e){t.stream=e,0==t.stream.getVideoTracks().length&&t.required_video?(t.onDeniedPermission({name:"Required Video",message:"No video device found."}),t.forgetPermission()):0==t.stream.getAudioTracks().length&&t.required_audio?(t.onDeniedPermission({name:"Required Audio",message:"No audio device found."}),t.forgetPermission()):(t.feed_in&&t.feed_in.setAttribute("src",URL.createObjectURL(t.stream)),t.onGetPermission&&"function"==typeof t.onGetPermission&&t.onGetPermission())}).then(null,t.onDeniedPermission&&"function"==typeof t.onDeniedPermission?t.onDeniedPermission:function(e){console.log(e.name+": "+e.message)})},requestMedia.prototype.startRecording=function(){var t=this;-1<this.browser.indexOf("Chrome")?(t.onStartRecording&&"function"==typeof t.onStartRecording&&t.onStartRecording(),t.recorder=new MediaRecorder(t.stream),t.recorder.ondataavailable=function(e){t.data_array.push(e.data)},t.recorder.start()):-1<t.browser.indexOf("Firefox")||-1<t.browser.indexOf("Opera")?(t.onStartRecording&&"function"==typeof t.onStartRecording&&t.onStartRecording(),t.recorder=new MediaRecorder(t.stream),t.recorder.start()):(t.onStartRecording&&"function"==typeof t.onStartRecording&&t.onStartRecording(),t.recorder=new MediaRecorder(t.stream),t.recorder.start(),t.recorder.ondataavailable=function(e){t.data_array.push(e.data)})},requestMedia.prototype.stopRecording=function(){var t=this;-1<t.browser.indexOf("Chrome")?(t.promise=new Promise(function(e){t.recorder.stop(),t.interval=setInterval(function(){0<t.data_array.length&&(clearInterval(t.interval),e(t.data_array))},2e3)}),t.promise.then(function(e){t.blob=new Blob(e,{type:t.file_type}),t.urlobj=URL.createObjectURL(t.blob),t.feed_out&&t.feed_out.setAttribute("src",URL.createObjectURL(t.blob)),t.onStopRecording&&"function"==typeof t.onStopRecording&&setTimeout(t.onStopRecording(),50),t.data_array=[]})):-1<t.browser.indexOf("Firefox")||-1<t.browser.indexOf("Opera")?(t.recorder.ondataavailable=function(e){t.urlobj=URL.createObjectURL(e.data),t.blob=new Blob([e.data],{type:t.file_type}),t.feed_out&&t.feed_out.setAttribute("src",URL.createObjectURL(e.data)),t.onStopRecording&&"function"==typeof t.onStopRecording&&setTimeout(t.onStopRecording(),50)},t.recorder.stop()):(t.recorder.stop(),t.blob=new Blob(t.data_array,{type:t.file_type}),t.urlobj=URL.createObjectURL(t.blob),t.feed_out&&t.feed_out.setAttribute("src",URL.createObjectURL(t.blob)),t.onStopRecording&&"function"==typeof t.onStopRecording&&setTimeout(t.onStopRecording(),50),t.data_array=[])},requestMedia.prototype.startCanvas=function(e){var t=this,o=e,r=0,e=null;t.getCanvas()&&(r=t.feed_in.videoHeight/(t.feed_in.videoWidth/o),isNaN(r)&&(r=9*o/12),t.canvas.setAttribute("width",o),t.canvas.setAttribute("height",r)),(e=t.canvas.getContext("2d")).fillStyle="#AAA",e.fillRect(0,0,t.canvas.width,t.canvas.height);e=t.canvas.toDataURL("image/png");t.feed_out.setAttribute("src",e)},requestMedia.prototype.takePicture=function(){var e=this,t=e.canvas.getContext("2d");if(e.canvas.width&&e.canvas.height){width=e.canvas.width,height=e.canvas.height,t.drawImage(e.feed_in,0,0,width,height);var o=e.canvas.toDataURL("image/png");e.feed_out.setAttribute("src",o);for(var t=(e.urlobj=o).split(";base64,"),o=t[0].split(":")[1],r=window.atob(t[1]),n=r.length,i=new Uint8Array(n),a=0;a<n;++a)i[a]=r.charCodeAt(a);e.blob=new Blob([i],{type:o}),e.onPictureTaken&&"function"==typeof e.onPictureTaken&&setTimeout(e.onPictureTaken(),50)}else console.log("erro"),console.log(e.canvas)},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.name,a.href=e.urlobj,a.textContent=a.download,document.body.appendChild(a),a.click(),setTimeout(function(){document.body.removeChild(a),URL.revokeObjectURL(a.href)},200)};