Gitlab CSE Unil

Commit ad10e43b authored by M. Chardon's avatar M. Chardon
Browse files

pb recherche micro

parent a93d6d3e
define(["jquery","mod_audioannotation/record"],function(a,b){function c(a){c.repo="repo"in a?a.repo:null,c.limit="limit"in a&&a.limit>0?1e3*a.limit:null,c.nbRendu="nbRendu"in a&&a.nbRendu>0?a.nbRendu:1,this.init()}c.constraints={audio:!0},c.sourceBuffer=null,c.mediaRecorder=null,c.recordedBlobs=null,c.repo=-1,c.limit=null,c.nbRendu=1,c.records=null,c.statut="off",c.prototype.mediaSource=null,c.pauseTimestamp=0,c.prototype.init=function(){c.records=[];for(var f=0;f<c.nbRendu;f++){var g=[];g.manager=this,g.limit=c.limit,g.repo=c.repo,g.indice=f,c.records[f]=new b(g)}if(a("#audioworkspace").remove(),c.mediaSource=new MediaSource,c.mediaSource.addEventListener("sourceopen",e,!1),!navigator.mediaDevices||!navigator.mediaDevices.getUserMedia)return void alert("getUserMedia() is not supported by your browser");navigator.permissions.query({name:"microphone"}).then(function(a){window.console.log(a.state),window.stream&&window.stream.getTracks().forEach(function(a){a.stop()})})["catch"](function(a){window.console.log("Got error :",a)});var h=null;navigator.mediaDevices.enumerateDevices().then(function(b){for(var e=0;e!==b.length;++e){var f=b[e];"audioinput"===f.kind?h=f.deviceId:window.console.log("Found another kind of device: ",f)}if(window.console.log(h),!h)return void alert("not available microphone");var g={audio:{deviceId:{exact:h}}},i=Date.now();navigator.mediaDevices.getUserMedia(g).then(function(a){window.console.log("Got stream, time diff :",Date.now()-i),d(a)})["catch"](function(b){alert("microphone no available"),window.console.log("GUM failed with error, time diff: ",Date.now()-i),window.console.error("navigator.getUserMedia error:",b),a(c.errorMsgElement).html("navigator.getUserMedia error:"+b.toString())})})};var d=function(b){a(".btn-record").each(function(){a(this).prop("indice")>=0&&c.records[a(this).prop("indice")].initDisplayRecordButton(!0)}),window.console.log("getUserMedia() got stream:",b),window.stream=b},e=function(){window.console.log("MediaSource opened"),c.sourceBuffer=c.mediaSource.addSourceBuffer("audio/ogg"),window.console.log("Source buffer: ",c.sourceBuffer)};c.prototype.startRecording=function(b){if("off"!==c.statut)return!1;c.statut="on",c.recordedBlobs=[],c.initTimestamp=-1,c.pauseTimestamp=0;var d=null;try{c.mediaRecorder=new MediaRecorder(window.stream,d)}catch(e){return window.console.error("Exception while creating MediaRecorder:",e),void a(this.errorMsgElement).html("Exception while creating MediaRecorder: "+JSON.stringify(e))}return window.console.log("Created MediaRecorder",c.mediaRecorder,"with options",d),c.mediaRecorder.addEventListener("stop",function(d){a("#audiotime"+b).text(""),c.timestampStop=0,window.console.log("Recorder stopped: ",d)},!1),c.mediaRecorder.addEventListener("dataavailable",function(a){f(a,b)},!1),c.mediaRecorder.start(10),window.console.log("MediaRecorder started",c.mediaRecorder),!0};var f=function(b,d){if(b.data&&b.data.size>0){c.initTimestamp<0&&(c.initTimestamp=b.timeStamp);var e=b.timeStamp-c.initTimestamp-c.pauseTimestamp,f=new Date(e);f.setHours(f.getHours()-1);var g=f.getHours()<10?"0"+f.getHours():f.getHours(),h=f.getMinutes()<10?"0"+f.getMinutes():f.getMinutes(),i=f.getSeconds()<10?"0"+f.getSeconds():f.getSeconds(),j=g+":"+h+":"+i;a("#audiotime"+d).text(j),c.limit&&e>c.limit&&(c.timestampStop&&0!==c.timestampStop||(c.timestampStop=1,c.records[d].stopRecord())),c.recordedBlobs.push(b.data)}};return c.prototype.stopRecording=function(a){c.statut="off",c.mediaRecorder.stop(),window.console.log("Recorded Blobs: ",c.recordedBlobs);var b=c.recordedBlobs;c.records[a].sendBlob(b),this.sendFile(b,a)},c.prototype.sendFile=function(b,d){a("#sending"+d).show();var e=new Blob(b),f=new window.FormData,g=(1e3*Math.random()).toString().replace(".","");f.append("repo_upload_file",e,g),f.append("itemid",g),f.append("title",g),f.append("repo_id",c.repo),f.append("env","editor"),f.append("sesskey",M.cfg.sesskey),f.append("savepath","/"),f.append("ctx_id",M.cfg.contextid);var h=M.cfg.wwwroot+"/repository/repository_ajax.php?action=upload",i=new XMLHttpRequest;i.addEventListener("readystatechange",function(){if(4===this.readyState&&200===this.status){a("#sending"+d).hide();var b=i.response,c=JSON.parse(b);a("input[name='filerecord"+d+"']").val(c.file),a("input[name='file"+d+"']").parent().parent().addClass("disabled"),a("#sendok"+d).show().fadeOut(3e3)}else this.readyState>200&&(a("#sending"+d).hide(),a("#sendko"+d).show())},!1),i.open("POST",h,!0),i.send(f)},c.prototype.pauseRecording=function(a){c.mediaRecorder&&(c.pauseBeginTimestamp=a.timeStamp,c.mediaRecorder.pause(),window.console.log("MediaRecorder paused",c.mediaRecorder))},c.prototype.resumeRecording=function(a){c.mediaRecorder&&(c.pauseTimestamp+=a.timeStamp-c.pauseBeginTimestamp,c.mediaRecorder.resume(),window.console.log("MediaRecorder resumed",c.mediaRecorder))},c.prototype.reset=function(){for(var a=0;a<c.records.length;a++)c.records[a].reset()},c});
\ No newline at end of file
define(["jquery","mod_audioannotation/record"],function(a,b){function c(a){c.repo="repo"in a?a.repo:null,c.limit="limit"in a&&a.limit>0?1e3*a.limit:null,c.nbRendu="nbRendu"in a&&a.nbRendu>0?a.nbRendu:1,this.init()}c.constraints={audio:!0},c.sourceBuffer=null,c.mediaRecorder=null,c.recordedBlobs=null,c.repo=-1,c.limit=null,c.nbRendu=1,c.records=null,c.statut="off",c.prototype.mediaSource=null,c.pauseTimestamp=0,c.prototype.init=function(){c.records=[];for(var f=0;f<c.nbRendu;f++){var g=[];g.manager=this,g.limit=c.limit,g.repo=c.repo,g.indice=f,c.records[f]=new b(g)}if(a("#audioworkspace").remove(),c.mediaSource=new MediaSource,c.mediaSource.addEventListener("sourceopen",e,!1),!navigator.mediaDevices||!window.MediaRecorder)return void alert("nowebrtc");if(!navigator.mediaDevices.getUserMedia)return void alert("getUserMedia is not supported by your browser");navigator.permissions.query({name:"microphone"}).then(function(a){window.console.log(a.state),window.stream&&window.stream.getTracks().forEach(function(a){a.stop()})})["catch"](function(a){window.console.log("Got error :",a)}),c.mediaRecorder&&"inactive"!==c.mediaRecorder.state&&c.mediaRecorder.stop(),window.stream&&window.stream.getTracks().forEach(function(a){"ended"!==a.readyState&&a.stop()});var h=null;navigator.mediaDevices.enumerateDevices().then(function(b){function e(a){window.console.log("Got stream, time diff :",Date.now()-l),d(a)}function f(a){m++;var b={audio:{deviceId:{exact:a}}};navigator.mediaDevices.getUserMedia(b).then(e)["catch"](g)}function g(b){i&&m<i.length?f(i[m]):(alert("microphone no available"),window.console.log("GUM failed with error, time diff: ",Date.now()-l),window.console.error("navigator.getUserMedia error:",b.name+": "+b.message),a(c.errorMsgElement).html("navigator.getUserMedia error:"+b.toString()))}var i=[];window.console.log(b);for(var j=0;j!==b.length;++j){var k=b[j];"audioinput"===k.kind&&(h=k.deviceId,window.console.log(h),i.push(h))}if(!h)return void alert("not available microphone");var l=Date.now(),m=0;navigator.mediaDevices.getUserMedia({audio:!0}).then(e)["catch"](g)})};var d=function(b){a(".btn-record").each(function(){a(this).prop("indice")>=0&&c.records[a(this).prop("indice")].initDisplayRecordButton(!0)}),window.console.log("getUserMedia() got stream:",b),window.stream=b},e=function(){window.console.log("MediaSource opened"),c.sourceBuffer=c.mediaSource.addSourceBuffer("audio/ogg"),window.console.log("Source buffer: ",c.sourceBuffer)};c.prototype.startRecording=function(b){if("off"!==c.statut)return!1;c.statut="on",c.recordedBlobs=[],c.initTimestamp=-1,c.pauseTimestamp=0;var d=null;try{c.mediaRecorder=new MediaRecorder(window.stream,d)}catch(e){return window.console.error("Exception while creating MediaRecorder:",e),void a(this.errorMsgElement).html("Exception while creating MediaRecorder: "+JSON.stringify(e))}return window.console.log("Created MediaRecorder",c.mediaRecorder,"with options",d),c.mediaRecorder.addEventListener("stop",function(d){a("#audiotime"+b).text(""),c.timestampStop=0,window.console.log("Recorder stopped: ",d)},!1),c.mediaRecorder.addEventListener("dataavailable",function(a){f(a,b)},!1),c.mediaRecorder.start(10),window.console.log("MediaRecorder started",c.mediaRecorder),!0};var f=function(b,d){if(b.data&&b.data.size>0){c.initTimestamp<0&&(c.initTimestamp=b.timeStamp);var e=b.timeStamp-c.initTimestamp-c.pauseTimestamp,f=new Date(e);f.setHours(f.getHours()-1);var g=f.getHours()<10?"0"+f.getHours():f.getHours(),h=f.getMinutes()<10?"0"+f.getMinutes():f.getMinutes(),i=f.getSeconds()<10?"0"+f.getSeconds():f.getSeconds(),j=g+":"+h+":"+i;a("#audiotime"+d).text(j),c.limit&&e>c.limit&&(c.timestampStop&&0!==c.timestampStop||(c.timestampStop=1,c.records[d].stopRecord())),c.recordedBlobs.push(b.data)}};return c.prototype.stopRecording=function(a){c.statut="off",c.mediaRecorder.stop(),window.console.log("Recorded Blobs: ",c.recordedBlobs);var b=c.recordedBlobs;c.records[a].sendBlob(b),this.sendFile(b,a)},c.prototype.sendFile=function(b,d){a("#sending"+d).show();var e=new Blob(b),f=new window.FormData,g=(1e3*Math.random()).toString().replace(".","");f.append("repo_upload_file",e,g),f.append("itemid",g),f.append("title",g),f.append("repo_id",c.repo),f.append("env","editor"),f.append("sesskey",M.cfg.sesskey),f.append("savepath","/"),f.append("ctx_id",M.cfg.contextid);var h=M.cfg.wwwroot+"/repository/repository_ajax.php?action=upload",i=new XMLHttpRequest;i.addEventListener("readystatechange",function(){if(4===this.readyState&&200===this.status){a("#sending"+d).hide();var b=i.response,c=JSON.parse(b);a("input[name='filerecord"+d+"']").val(c.file),a("input[name='file"+d+"']").parent().parent().addClass("disabled"),a("#sendok"+d).show().fadeOut(3e3)}else this.readyState>200&&(a("#sending"+d).hide(),a("#sendko"+d).show())},!1),i.open("POST",h,!0),i.send(f)},c.prototype.pauseRecording=function(a){c.mediaRecorder&&(c.pauseBeginTimestamp=a.timeStamp,c.mediaRecorder.pause(),window.console.log("MediaRecorder paused",c.mediaRecorder))},c.prototype.resumeRecording=function(a){c.mediaRecorder&&(c.pauseTimestamp+=a.timeStamp-c.pauseBeginTimestamp,c.mediaRecorder.resume(),window.console.log("MediaRecorder resumed",c.mediaRecorder))},c.prototype.reset=function(){for(var a=0;a<c.records.length;a++)c.records[a].reset()},c});
\ No newline at end of file
......@@ -39,19 +39,13 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.sourceBuffer = null;
RecordsManager.mediaRecorder = null;
RecordsManager.recordedBlobs = null;
RecordsManager.repo = -1;
RecordsManager.limit = null;
RecordsManager.nbRendu = 1;
RecordsManager.records = null;
RecordsManager.statut = 'off';
RecordsManager.prototype.mediaSource = null;
RecordsManager.pauseTimestamp = 0;
/********************************
* CONSTRUCTOR *
********************************/
......@@ -87,14 +81,18 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.records[i] = new Record(parameters);
}
$("#audioworkspace").remove();
RecordsManager.mediaSource = new MediaSource();
//RecordsManager.mediaSource.setMaxDuration(60000); // Set max duration 60 sec.
//RecordsManager.mediaSource.setMaxFileSize(5000000); // Set max file size 5M
RecordsManager.mediaSource.addEventListener('sourceopen', handleSourceOpen, false);
// Show alert and close plugin if WebRTC is not supported.
// Older browsers might not implement mediaDevices at all
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
if (!(navigator.mediaDevices && window.MediaRecorder)) {
alert('nowebrtc');
return;
}
if (navigator.mediaDevices.getUserMedia) {
// Good to go!
//check for chrome of already accept permission
navigator.permissions.query({name: 'microphone'})
......@@ -109,58 +107,75 @@ define(['jquery', 'mod_audioannotation/record'],
.catch(function (error) {
window.console.log('Got error :', error);
});
//reset stream if already here
if (RecordsManager.mediaRecorder && RecordsManager.mediaRecorder.state !== 'inactive') {
RecordsManager.mediaRecorder.stop();
}
if (window.stream) {
window.stream.getTracks().forEach(function (track) {
if (track.readyState !== 'ended') {
track.stop();
}
});
}
var audiodeviceid = null;
navigator.mediaDevices.enumerateDevices()
.then(function (deviceInfos) {
var listDevices = [];
window.console.log(deviceInfos);
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
if (deviceInfo.kind === 'audioinput') {
audiodeviceid = deviceInfo.deviceId;
} else {
window.console.log('Found another kind of device: ', deviceInfo);
window.console.log(audiodeviceid);
listDevices.push(audiodeviceid);
}
}
window.console.log(audiodeviceid);
//on verifie si on a au moins 1 micro dispo
if (!audiodeviceid) {
alert('not available microphone');
return;
}
var constraints = {
audio: {
deviceId: {exact: audiodeviceid}
}
};
//wait for the user to allow its mic
// In the Promise handlers, if Date.now() - now < 500
// then we can assume this is a persisted user setting
var now = Date.now();
navigator.mediaDevices.getUserMedia(constraints).then(
function (stream) {
window.console.log('Got stream, time diff :', Date.now() - now);
handleSuccess(stream);
})
.catch(
function (e) {
alert('microphone no available');
window.console.log('GUM failed with error, time diff: ', Date.now() - now);
window.console.error('navigator.getUserMedia error:', e);
$(RecordsManager.errorMsgElement)
.html('navigator.getUserMedia error:' + e.toString());
}
);
});
var pointeur = 0;
function gotStream(stream) {
window.console.log('Got stream, time diff :', Date.now() - now);
handleSuccess(stream);
}
function tryGetUserMedia(value) {
pointeur++;
var constraints = {
audio: {
deviceId: {exact: value}
}
};
navigator.mediaDevices.getUserMedia(constraints).then(gotStream).catch(errorStream);
}
function errorStream(e) {
if (listDevices && pointeur < listDevices.length) {
tryGetUserMedia(listDevices[pointeur]);
} else {
alert('microphone no available');
window.console.log('GUM failed with error, time diff: ', Date.now() - now);
window.console.error('navigator.getUserMedia error:', e.name + ": " + e.message);
$(RecordsManager.errorMsgElement)
.html('navigator.getUserMedia error:' + e.toString());
}
}
//wait for the user to allow its mic
navigator.mediaDevices.getUserMedia({audio: true}).then(gotStream).catch(errorStream);
});
} else {
alert('getUserMedia() is not supported by your browser');
alert('getUserMedia is not supported by your browser');
return;
}
};
/**
* Close MediaSource
* @param {UserMedia} stream
......@@ -175,7 +190,6 @@ define(['jquery', 'mod_audioannotation/record'],
window.console.log('getUserMedia() got stream:', stream);
window.stream = stream;
};
/**
* Open MediaSource
* @returns {undefined}
......@@ -185,7 +199,6 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.sourceBuffer = RecordsManager.mediaSource.addSourceBuffer('audio/ogg');
window.console.log('Source buffer: ', RecordsManager.sourceBuffer);
};
/**
* Start record stream
*/
......@@ -212,7 +225,6 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.timestampStop = 0;
window.console.log('Recorder stopped: ', event);
}, false);
//this.mediaRecorder.ondataavailable = handleDataAvailable;
RecordsManager.mediaRecorder.addEventListener('dataavailable', function (event) {
handleDataAvailable(event, indice);
......@@ -221,7 +233,6 @@ define(['jquery', 'mod_audioannotation/record'],
window.console.log('MediaRecorder started', RecordsManager.mediaRecorder);
return true;
};
/**
* Build entire stream
* @param {Event} event
......@@ -248,7 +259,6 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.recordedBlobs.push(event.data);
}
};
/**
* Stop record stream
*/
......@@ -260,7 +270,6 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.records[indice].sendBlob(blobRaw);
this.sendFile(blobRaw, indice);
};
/**
* Create a formData, and send the current stream to Moodle with Moodle API
*/
......@@ -299,9 +308,7 @@ define(['jquery', 'mod_audioannotation/record'],
}, false);
xhr.open("POST", uploadEndpoint, true);
xhr.send(formData);
};
/**
* Pause record stream
*/
......@@ -313,7 +320,6 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.mediaRecorder.pause();
window.console.log('MediaRecorder paused', RecordsManager.mediaRecorder);
};
/**
* Resume record stream
*/
......@@ -324,9 +330,7 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.pauseTimestamp += event.timeStamp - RecordsManager.pauseBeginTimestamp;
RecordsManager.mediaRecorder.resume();
window.console.log('MediaRecorder resumed', RecordsManager.mediaRecorder);
};
/**
* Reset recorders
*/
......@@ -335,8 +339,5 @@ define(['jquery', 'mod_audioannotation/record'],
RecordsManager.records[i].reset();
}
};
return RecordsManager;
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment