306 lines
69 KiB
JavaScript
306 lines
69 KiB
JavaScript
|
|
||
|
/*!
|
||
|
* Socket.IO v2.2.0
|
||
|
* (c) 2014-2018 Guillermo Rauch
|
||
|
* Released under the MIT License.
|
||
|
*/
|
||
|
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.io=e():t.io=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function r(t,e){"object"===("undefined"==typeof t?"undefined":o(t))&&(e=t,t=void 0),e=e||{};var n,r=i(t),s=r.source,u=r.id,h=r.path,f=p[u]&&h in p[u].nsps,l=e.forceNew||e["force new connection"]||!1===e.multiplex||f;return l?(c("ignoring socket cache for %s",s),n=a(s,e)):(p[u]||(c("new io instance for %s",s),p[u]=a(s,e)),n=p[u]),r.query&&!e.query&&(e.query=r.query),n.socket(r.path,e)}var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i=n(1),s=n(7),a=n(12),c=n(3)("socket.io-client");t.exports=e=r;var p=e.managers={};e.protocol=s.protocol,e.connect=r,e.Manager=n(12),e.Socket=n(36)},function(t,e,n){"use strict";function r(t,e){var n=t;e=e||"undefined"!=typeof location&&location,null==t&&(t=e.protocol+"//"+e.host),"string"==typeof t&&("/"===t.charAt(0)&&(t="/"===t.charAt(1)?e.protocol+t:e.host+t),/^(https?|wss?):\/\//.test(t)||(i("protocol-less url %s",t),t="undefined"!=typeof e?e.protocol+"//"+t:"https://"+t),i("parse %s",t),n=o(t)),n.port||(/^(http|ws)$/.test(n.protocol)?n.port="80":/^(http|ws)s$/.test(n.protocol)&&(n.port="443")),n.path=n.path||"/";var r=n.host.indexOf(":")!==-1,s=r?"["+n.host+"]":n.host;return n.id=n.protocol+"://"+s+":"+n.port,n.href=n.protocol+"://"+s+(e&&e.port===n.port?"":":"+n.port),n}var o=n(2),i=n(3)("socket.io-client:url");t.exports=r},function(t,e){var n=/^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/,r=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"];t.exports=function(t){var e=t,o=t.indexOf("["),i=t.indexOf("]");o!=-1&&i!=-1&&(t=t.substring(0,o)+t.substring(o,i).replace(/:/g,";")+t.substring(i,t.length));for(var s=n.exec(t||""),a={},c=14;c--;)a[r[c]]=s[c]||"";return o!=-1&&i!=-1&&(a.source=e,a.host=a.host.substring(1,a.host.length-1).replace(/;/g,":"),a.authority=a.authority.replace("[","").replace("]","").replace(/;/g,":"),a.ipv6uri=!0),a}},function(t,e,n){(function(r){function o(){return!("undefined"==typeof window||!window.process||"renderer"!==window.process.type)||("undefined"==typeof navigator||!navigator.userAgent||!navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/))&&("undefined"!=typeof document&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||"undefined"!=typeof window&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/))}function i(t){var n=this.useColors;if(t[0]=(n?"%c":"")+this.namespace+(n?" %c":" ")+t[0]+(n?"%c ":" ")+"+"+e.humanize(this.diff),n){var r="color: "+this.color;t.splice(1,0,r,"color: inherit");var o=0,i=0;t[0].replace(/%[a-zA-Z%]/g,function(t){"%%"!==t&&(o++,"%c"===t&&(i=o))}),t.splice(i,0,r)}}function s(){return"object"==typeof console&&console.log&&Function.prototype.apply.call(console.log,console,arguments)}function a(t){try{null==t?e.storage.removeItem("debug"):e.storage.debug=t}catch(n){}}function c(){var t;try{t=e.storage.debug}catch(n){}return!t&&"undefined"!=typeof r&&"env"in r&&(t=r.env.DEBUG),t}function p(){try{return window.localStorage}catch(t){}}e=t.ex
|
||
|
if("undefined"!=typeof location){var c="https:"===location.protocol,p=location.port;p||(p=c?443:80),n=t.hostname!==location.hostname||p!==t.port,r=t.secure!==c}if(t.xdomain=n,t.xscheme=r,e=new o(t),"open"in e&&!t.forceJSONP)return new i(t);if(!a)throw new Error("JSONP disabled");return new s(t)}var o=n(16),i=n(18),s=n(32),a=n(33);e.polling=r,e.websocket=a},function(t,e,n){var r=n(17);t.exports=function(t){var e=t.xdomain,n=t.xscheme,o=t.enablesXDR;try{if("undefined"!=typeof XMLHttpRequest&&(!e||r))return new XMLHttpRequest}catch(i){}try{if("undefined"!=typeof XDomainRequest&&!n&&o)return new XDomainRequest}catch(i){}if(!e)try{return new(self[["Active"].concat("Object").join("X")])("Microsoft.XMLHTTP")}catch(i){}}},function(t,e){try{t.exports="undefined"!=typeof XMLHttpRequest&&"withCredentials"in new XMLHttpRequest}catch(n){t.exports=!1}},function(t,e,n){function r(){}function o(t){if(c.call(this,t),this.requestTimeout=t.requestTimeout,this.extraHeaders=t.extraHeaders,"undefined"!=typeof location){var e="https:"===location.protocol,n=location.port;n||(n=e?443:80),this.xd="undefined"!=typeof location&&t.hostname!==location.hostname||n!==t.port,this.xs=t.secure!==e}}function i(t){this.method=t.method||"GET",this.uri=t.uri,this.xd=!!t.xd,this.xs=!!t.xs,this.async=!1!==t.async,this.data=void 0!==t.data?t.data:null,this.agent=t.agent,this.isBinary=t.isBinary,this.supportsBinary=t.supportsBinary,this.enablesXDR=t.enablesXDR,this.requestTimeout=t.requestTimeout,this.pfx=t.pfx,this.key=t.key,this.passphrase=t.passphrase,this.cert=t.cert,this.ca=t.ca,this.ciphers=t.ciphers,this.rejectUnauthorized=t.rejectUnauthorized,this.extraHeaders=t.extraHeaders,this.create()}function s(){for(var t in i.requests)i.requests.hasOwnProperty(t)&&i.requests[t].abort()}var a=n(16),c=n(19),p=n(8),u=n(30),h=n(3)("engine.io-client:polling-xhr");if(t.exports=o,t.exports.Request=i,u(o,c),o.prototype.supportsBinary=!0,o.prototype.request=function(t){return t=t||{},t.uri=this.uri(),t.xd=this.xd,t.xs=this.xs,t.agent=this.agent||!1,t.supportsBinary=this.supportsBinary,t.enablesXDR=this.enablesXDR,t.pfx=this.pfx,t.key=this.key,t.passphrase=this.passphrase,t.cert=this.cert,t.ca=this.ca,t.ciphers=this.ciphers,t.rejectUnauthorized=this.rejectUnauthorized,t.requestTimeout=this.requestTimeout,t.extraHeaders=this.extraHeaders,new i(t)},o.prototype.doWrite=function(t,e){var n="string"!=typeof t&&void 0!==t,r=this.request({method:"POST",data:t,isBinary:n}),o=this;r.on("success",e),r.on("error",function(t){o.onError("xhr post error",t)}),this.sendXhr=r},o.prototype.doPoll=function(){h("xhr poll");var t=this.request(),e=this;t.on("data",function(t){e.onData(t)}),t.on("error",function(t){e.onError("xhr poll error",t)}),this.pollXhr=t},p(i.prototype),i.prototype.create=function(){var t={agent:this.agent,xdomain:this.xd,xscheme:this.xs,enablesXDR:this.enablesXDR};t.pfx=this.pfx,t.key=this.key,t.passphrase=this.passphrase,t.cert=this.cert,t.ca=this.ca,t.ciphers=this.ciphers,t.rejectUnauthorized=this.rejectUnauthorized;var e=this.xhr=new a(t),n=this;try{h("xhr open %s: %s",this.method,this.uri),e.open(this.method,this.uri,this.async);try{if(this.extraHeaders){e.setDisableHeaderCheck&&e.setDisableHeaderCheck(!0);for(var r in this.extraHeaders)this.extraHeaders.hasOwnProperty(r)&&e.setRequestHeader(r,this.extraHeaders[r])}}catch(o){}if("POST"===this.method)try{this.isBinary?e.setRequestHeader("Content-type","application/octet-stream"):e.setRequestHeader("Content-type","text/plain;charset=UTF-8")}catch(o){}try{e.setRequestHeader("Accept","*/*")}catch(o){}"withCredentials"in e&&(e.withCredentials=!0),this.requestTimeout&&(e.timeout=this.requestTimeout),this.hasXDR()?(e.onload=function(){n.onLoad()},e.onerror=function(){n.onError(e.responseText)}):e.onreadystatechange=function(){if(2===e.readyState)try{var t=e.getResponseHeader("Content-Type");n.supportsBinary&&"application/octet-stream"===t&&(e.responseType="arraybuffer")}catch(r){}4===e.readyState&&(200===e.status||1223===e.status?n.onLoad():setTimeout(function(){n.onError(e.status)},0))},h("xhr data %s",this.data),e.send(th
|
||
|
var h,f,l,d=String.fromCharCode;t.exports={version:"2.1.2",encode:a,decode:u}},function(t,e){!function(){"use strict";for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",n=new Uint8Array(256),r=0;r<t.length;r++)n[t.charCodeAt(r)]=r;e.encode=function(e){var n,r=new Uint8Array(e),o=r.length,i="";for(n=0;n<o;n+=3)i+=t[r[n]>>2],i+=t[(3&r[n])<<4|r[n+1]>>4],i+=t[(15&r[n+1])<<2|r[n+2]>>6],i+=t[63&r[n+2]];return o%3===2?i=i.substring(0,i.length-1)+"=":o%3===1&&(i=i.substring(0,i.length-2)+"=="),i},e.decode=function(t){var e,r,o,i,s,a=.75*t.length,c=t.length,p=0;"="===t[t.length-1]&&(a--,"="===t[t.length-2]&&a--);var u=new ArrayBuffer(a),h=new Uint8Array(u);for(e=0;e<c;e+=4)r=n[t.charCodeAt(e)],o=n[t.charCodeAt(e+1)],i=n[t.charCodeAt(e+2)],s=n[t.charCodeAt(e+3)],h[p++]=r<<2|o>>4,h[p++]=(15&o)<<4|i>>2,h[p++]=(3&i)<<6|63&s;return u}}()},function(t,e){function n(t){return t.map(function(t){if(t.buffer instanceof ArrayBuffer){var e=t.buffer;if(t.byteLength!==e.byteLength){var n=new Uint8Array(t.byteLength);n.set(new Uint8Array(e,t.byteOffset,t.byteLength)),e=n.buffer}return e}return t})}function r(t,e){e=e||{};var r=new i;return n(t).forEach(function(t){r.append(t)}),e.type?r.getBlob(e.type):r.getBlob()}function o(t,e){return new Blob(n(t),e||{})}var i="undefined"!=typeof i?i:"undefined"!=typeof WebKitBlobBuilder?WebKitBlobBuilder:"undefined"!=typeof MSBlobBuilder?MSBlobBuilder:"undefined"!=typeof MozBlobBuilder&&MozBlobBuilder,s=function(){try{var t=new Blob(["hi"]);return 2===t.size}catch(e){return!1}}(),a=s&&function(){try{var t=new Blob([new Uint8Array([1,2])]);return 2===t.size}catch(e){return!1}}(),c=i&&i.prototype.append&&i.prototype.getBlob;"undefined"!=typeof Blob&&(r.prototype=Blob.prototype,o.prototype=Blob.prototype),t.exports=function(){return s?a?Blob:o:c?r:void 0}()},function(t,e){e.encode=function(t){var e="";for(var n in t)t.hasOwnProperty(n)&&(e.length&&(e+="&"),e+=encodeURIComponent(n)+"="+encodeURIComponent(t[n]));return e},e.decode=function(t){for(var e={},n=t.split("&"),r=0,o=n.length;r<o;r++){var i=n[r].split("=");e[decodeURIComponent(i[0])]=decodeURIComponent(i[1])}return e}},function(t,e){t.exports=function(t,e){var n=function(){};n.prototype=e.prototype,t.prototype=new n,t.prototype.constructor=t}},function(t,e){"use strict";function n(t){var e="";do e=s[t%a]+e,t=Math.floor(t/a);while(t>0);return e}function r(t){var e=0;for(u=0;u<t.length;u++)e=e*a+c[t.charAt(u)];return e}function o(){var t=n(+new Date);return t!==i?(p=0,i=t):t+"."+n(p++)}for(var i,s="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_".split(""),a=64,c={},p=0,u=0;u<a;u++)c[s[u]]=u;o.encode=n,o.decode=r,t.exports=o},function(t,e,n){(function(e){function r(){}function o(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof e?e:{}}function i(t){if(s.call(this,t),this.query=this.query||{},!c){var e=o();c=e.___eio=e.___eio||[]}this.index=c.length;var n=this;c.push(function(t){n.onData(t)}),this.query.j=this.index,"function"==typeof addEventListener&&addEventListener("beforeunload",function(){n.script&&(n.script.onerror=r)},!1)}var s=n(19),a=n(30);t.exports=i;var c,p=/\n/g,u=/\\n/g;a(i,s),i.prototype.supportsBinary=!1,i.prototype.doClose=function(){this.script&&(this.script.parentNode.removeChild(this.script),this.script=null),this.form&&(this.form.parentNode.removeChild(this.form),this.form=null,this.iframe=null),s.prototype.doClose.call(this)},i.prototype.doPoll=function(){var t=this,e=document.createElement("script");this.script&&(this.script.parentNode.removeChild(this.script),this.script=null),e.async=!0,e.src=this.uri(),e.onerror=function(e){t.onError("jsonp poll error",e)};var n=document.getElementsByTagName("script")[0];n?n.parentNode.insertBefore(e,n):(document.head||document.body).appendChild(e),this.script=e;var r="undefined"!=typeof navigator&&/gecko/i.test(navigator.userAgent);r&&setTimeout(function(){var t=document.createElement("iframe");document.body.appendChild(t),document.body.removeChild(t)},100)},i.prototype.doWrite=function(t,e){function n(){r(),e()}
|
||
|
//# sourceMappingURL=socket.io.js.map
|
||
|
|
||
|
|
||
|
// document.write('<script src = "node_modules/socket.io/index.js"></script>');
|
||
|
|
||
|
|
||
|
var socket = io('http://localhost:3000');
|
||
|
listeners = {}
|
||
|
var front = {
|
||
|
send:send,
|
||
|
on:on,
|
||
|
sendSync:sendSync
|
||
|
}
|
||
|
|
||
|
function addListener(event, fn) {
|
||
|
listeners[event] = listeners[event] || [];
|
||
|
listeners[event].push(fn);
|
||
|
}
|
||
|
|
||
|
function on(event, fn){
|
||
|
addListener(event, fn);
|
||
|
}
|
||
|
|
||
|
function exeFunction(event, ...args){
|
||
|
let fns = listeners[event];
|
||
|
if (!fns) return false;
|
||
|
fns.forEach(function (f) {
|
||
|
f(...args);
|
||
|
});
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
function send(event, ...args){
|
||
|
// console.log('called send');
|
||
|
socket.emit('response-from-front', event, ...args)
|
||
|
}
|
||
|
|
||
|
socket.on('response-from-back', function(event, ...args){
|
||
|
// console.log('reposen sync')
|
||
|
exeFunction(event, ...args);
|
||
|
})
|
||
|
|
||
|
socket.on('sync-response', function(eventName, res){
|
||
|
// console.log('sync-reponse')
|
||
|
// console.log(eventName);
|
||
|
exeFunction(eventName + "-result", res);
|
||
|
})
|
||
|
|
||
|
function sendSync(event, ...args){
|
||
|
socket.emit('sync-response-from-front', event, ...args);
|
||
|
}
|
||
|
|
||
|
var app = {}
|
||
|
|
||
|
/**
|
||
|
* Camera API
|
||
|
*/
|
||
|
|
||
|
var camera = {
|
||
|
init : initCamera,
|
||
|
startRecording: startVideoRecording,
|
||
|
stopRecording: stopVideoRecording,
|
||
|
saveRecording: saveVideoRecording,
|
||
|
previewRecording: previewVideoRecordingIntoDom,
|
||
|
getDevices : getVideoDevices,
|
||
|
getBuffer: getCameraRecordedBuffer,
|
||
|
takePhoto: takePhoto,
|
||
|
savePhoto: saveCameraPhoto,
|
||
|
recordedBlobs : [],
|
||
|
mediaRecorder: null,
|
||
|
canvas : document.createElement('canvas')
|
||
|
}
|
||
|
|
||
|
function initCamera(dom, constraints){
|
||
|
if(window.videoStream != undefined){
|
||
|
stopMediaTracks(window.videoStream);
|
||
|
}
|
||
|
navigator.getUserMedia(constraints, function(stream){
|
||
|
window.videoStream = stream;
|
||
|
dom.srcObject = stream;
|
||
|
}, function(error){
|
||
|
console.log(error);
|
||
|
throw error;
|
||
|
})
|
||
|
}
|
||
|
|
||
|
function handleCameraDataAvailable(event) {
|
||
|
if (event.data && event.data.size > 0) {
|
||
|
camera.recordedBlobs.push(event.data);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function startVideoRecording(options){
|
||
|
camera.recordedBlobs = [];
|
||
|
try {
|
||
|
camera.mediaRecorder = new MediaRecorder(window.videoStream, options);
|
||
|
} catch (e0) {
|
||
|
console.log('Unable to create MediaRecorder with options Object: ', options, e0);
|
||
|
try {
|
||
|
options = {mimeType: 'video/webm;codecs=vp8', bitsPerSecond: 100000};
|
||
|
camera.mediaRecorder = new MediaRecorder(window.videoStream, options);
|
||
|
} catch (e1) {
|
||
|
console.log('Unable to create MediaRecorder with options Object: ', options, e1);
|
||
|
try {
|
||
|
options = 'video/mp4';
|
||
|
camera.mediaRecorder = new MediaRecorder(window.videoStream, options);
|
||
|
} catch (e2) {
|
||
|
alert('MediaRecorder is not supported by this browser.');
|
||
|
console.error('Exception while creating MediaRecorder:', e2);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// mediaRecorder.onstop = handleStop;
|
||
|
camera.mediaRecorder.ondataavailable = handleCameraDataAvailable;
|
||
|
camera.mediaRecorder.start(); // collect 10ms of data
|
||
|
}
|
||
|
|
||
|
function stopVideoRecording(){
|
||
|
camera.mediaRecorder.stop();
|
||
|
}
|
||
|
|
||
|
function saveVideoRecording(filepath, filename, options){
|
||
|
let blob = new Blob(camera.recordedBlobs, options);
|
||
|
saveCameraBlob(filepath, filename, blob);
|
||
|
}
|
||
|
|
||
|
function previewVideoRecordingIntoDom(dom, options){
|
||
|
dom.controls = true;
|
||
|
let superBuffer = new Blob(camera.recordedBlobs, options);
|
||
|
dom.src = window.URL.createObjectURL(superBuffer);
|
||
|
}
|
||
|
|
||
|
function saveCameraBlob(filepath, filename, blob){
|
||
|
let reader = new FileReader()
|
||
|
reader.onload = function() {
|
||
|
if (reader.readyState == 2) {
|
||
|
send('androidjs:saveBlob', filepath, filename, reader.result, 'video');
|
||
|
console.log(`Saving ${JSON.stringify({ filename, size: blob.size })}`)
|
||
|
}
|
||
|
}
|
||
|
reader.readAsArrayBuffer(blob)
|
||
|
}
|
||
|
|
||
|
function getVideoDevices(callback){
|
||
|
let devices = []
|
||
|
navigator.mediaDevices.enumerateDevices()
|
||
|
.then(function(mediaDevices){
|
||
|
for(let i = 0; i < mediaDevices.length; i++){
|
||
|
if(mediaDevices[i].kind === 'videoinput'){
|
||
|
devices.push(mediaDevices[i].deviceId);
|
||
|
}
|
||
|
}
|
||
|
callback(devices);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function stopMediaTracks(stream) {
|
||
|
stream.getTracks().forEach(track => {
|
||
|
track.stop();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function getCameraRecordedBuffer(options, callback){
|
||
|
let blob = new Blob(camera.recordedBlobs, options);
|
||
|
let reader = new FileReader()
|
||
|
reader.onload = function() {
|
||
|
if (reader.readyState == 2) {
|
||
|
callback(reader.result);
|
||
|
}
|
||
|
}
|
||
|
reader.readAsArrayBuffer(blob)
|
||
|
}
|
||
|
|
||
|
function takePhoto(cameraDom, previewDom){
|
||
|
camera.canvas.width = cameraDom.videoWidth;
|
||
|
camera.canvas.height = cameraDom.videoHeight;
|
||
|
camera.canvas.getContext('2d').drawImage(cameraDom, 0, 0);
|
||
|
if(previewDom != undefined){
|
||
|
previewDom.src = camera.canvas.toDataURL('image/webp');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function saveCameraPhoto(filepath, filename){
|
||
|
let data = camera.canvas.toDataURL('image/webp').replace(/^data:image\/\w+;base64,/, "");
|
||
|
send('androidjs:saveBlob', filepath, filename, data, 'image');
|
||
|
console.log('saving file...');
|
||
|
}
|
||
|
|
||
|
app.camera = camera;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Microphone API
|
||
|
*/
|
||
|
|
||
|
microphone = {
|
||
|
startRecording: startAudioRecording,
|
||
|
getDevices: getAudioDevices,
|
||
|
stopRecording: stopAudioRecording,
|
||
|
previewRecording: previewAudioRecording,
|
||
|
saveRecording: saveAudioRecording,
|
||
|
getBuffer: getAudioRecordedBuffer,
|
||
|
recordedBlobs: [],
|
||
|
mediaRecorder: null
|
||
|
}
|
||
|
|
||
|
function handleAudioDataAvailable(event){
|
||
|
if (event.data && event.data.size > 0) {
|
||
|
microphone.recordedBlobs.push(event.data);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function startAudioRecording(options){
|
||
|
if(window.audioStream != undefined){
|
||
|
stopMediaTracks(window.audioStream);
|
||
|
}
|
||
|
navigator.getUserMedia(options, function(stream){
|
||
|
window.audioStream = stream;
|
||
|
microphone.mediaRecorder = new MediaRecorder(window.audioStream);
|
||
|
microphone.mediaRecorder.ondataavailable = handleAudioDataAvailable;
|
||
|
microphone.mediaRecorder.start();
|
||
|
}, function(error){
|
||
|
console.log(error);
|
||
|
throw error;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function stopAudioRecording(){
|
||
|
microphone.mediaRecorder.stop();
|
||
|
}
|
||
|
|
||
|
function previewAudioRecording(dom, options){
|
||
|
dom.controls = true;
|
||
|
let blob = new Blob(microphone.recordedBlobs, options);
|
||
|
dom.src = URL.createObjectURL(blob);
|
||
|
}
|
||
|
|
||
|
function saveAudioRecording(filepath, filename, options){
|
||
|
let blob = new Blob(microphone.recordedBlobs, options);
|
||
|
saveAudioBlob(filepath, filename, blob);
|
||
|
}
|
||
|
|
||
|
function saveAudioBlob(filepath, filename, blob){
|
||
|
let reader = new FileReader()
|
||
|
reader.onload = function() {
|
||
|
if (reader.readyState == 2) {
|
||
|
send('androidjs:saveBlob', filepath, filename, reader.result, 'audio');
|
||
|
console.log(`Saving ${JSON.stringify({ filename, size: blob.size })}`)
|
||
|
}
|
||
|
}
|
||
|
reader.readAsArrayBuffer(blob);
|
||
|
}
|
||
|
|
||
|
function getAudioRecordedBuffer(options, callback){
|
||
|
let blob = new Blob(microphone.recordedBlobs, options);
|
||
|
let reader = new FileReader();
|
||
|
reader.onload = function(){
|
||
|
if(reader.readyState == 2){
|
||
|
callback(reader.result);
|
||
|
}
|
||
|
}
|
||
|
reader.readAsArrayBuffer(blob);
|
||
|
}
|
||
|
|
||
|
function getAudioDevices(callback){
|
||
|
let devices = [];
|
||
|
navigator.mediaDevices.enumerateDevices()
|
||
|
.then(function(mediaDevices){
|
||
|
for(let i = 0; i < mediaDevices.length; i++){
|
||
|
if(mediaDevices[i].kind == 'audioinput'){
|
||
|
devices.push(mediaDevices[i].deviceId);
|
||
|
}
|
||
|
}
|
||
|
callback(devices);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
app.microphone = microphone;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* App Module
|
||
|
*/
|
||
|
|
||
|
app.getPath = function(name){
|
||
|
return window.android.getPath(name);
|
||
|
}
|
||
|
|
||
|
app.loadURL = function(url){
|
||
|
window.href = url;
|
||
|
}
|
||
|
|
||
|
app.reload = function(){
|
||
|
window.reload();
|
||
|
}
|