record_voice.tpl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. {# <script type="text/javascript" src="{{ _p.web_lib }}javascript/rtc/RecordRTC.js"></script> #}
  2. <script type="text/javascript" src="{{ _p.web_lib }}swfobject/swfobject.js"></script>
  3. <script type="text/javascript" src="{{ _p.web_lib }}wami-recorder/recorder.js"></script>
  4. <script type="text/javascript" src="{{ _p.web_lib }}wami-recorder/gui.js"></script>
  5. <div id="rtc">
  6. <audio class="skip" id="audio" autoplay="" loop="" controls=""></audio>
  7. <span id="progress-info"></span>
  8. <br />
  9. <a id="record" class="btn btn-danger" >{{ 'RecordAudio' | get_lang }}</a>
  10. <button id="stop" class="btn btn-default" disabled="">{{ 'StopRecordingAudio' | get_lang }}</button>
  11. </div>
  12. <div id="wami" style="float:left; z-index:6000">
  13. <a id="record-wami" class="btn btn-info">{{ 'ActivateAudioRecorder' | get_lang }}</a>
  14. <br />
  15. <div id="wami-recorder">
  16. </div>
  17. <div id="start-recording" class="alert" style="display:none">
  18. {{ "StartSpeaking" | get_lang }}
  19. </div>
  20. </div>
  21. <div id="nanogong">
  22. <applet id="nanogongApplet" archive="{{ _p.web_lib }}nanogong/nanogong.jar" code="gong.NanoGong" width="250" height="95">
  23. <param name="ShowTime" value="true" />
  24. <param name="AudioFormat" value="ImaADPCM" />
  25. <param name="ShowSpeedButton" value="false" />
  26. </applet>
  27. <form name="form_nanogong">
  28. <input id="status" type="hidden" name="status" value="0">
  29. <button class="upload" type="submit" value="{{ 'Send' | get_lang }}" onclick="submitVoice()">
  30. {{ 'Send' | get_lang }}
  31. </button>
  32. </form>
  33. <span id="nanogong_result" ></span>
  34. </div>
  35. <span id="record-result"></span>
  36. <div class="clear"></div>
  37. <script>
  38. function checkNanoGongEnable() {
  39. var nanogongEnabled = {{ enable_nanogong }};
  40. if (nanogongEnabled != 1) {
  41. return false;
  42. }
  43. return recorder = document.getElementById("nanogongApplet");
  44. }
  45. function checkWamiEnable() {
  46. var wamiEnabled = {{ enable_wami }};
  47. if (wamiEnabled != 1) {
  48. return false;
  49. }
  50. if (swfobject.hasFlashPlayerVersion("1")) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. function submitVoice() {
  57. // lang vars
  58. var lang_no_applet = "{{ 'NanogongNoApplet' | get_lang }}";
  59. var lang_record_before_save = "{{ 'NanogongRecordBeforeSave' | get_lang }}";
  60. var lang_give_a_title = "{{ 'NanogongGiveTitle' | get_lang }}";
  61. var lang_failled_to_submit = "{{ 'NanogongFailledToSubmit' | get_lang }}";
  62. var lang_submitted = "{{'NanogongSubmitted' | get_lang }}";
  63. // user and group id
  64. //path, url and filename
  65. var filename = "{{ filename }}"; //adding name file, tag and extension
  66. var filename = encodeURIComponent(filename);
  67. var fileInput = 'file';
  68. var urlNanoGong = "{{ _p.web_lib }}nanogong/upload_nanogong_file.php?lp_item_id={{ lp_item_id }}&filename="+filename+"&file_field="+fileInput+"&cidReq={{ course_code }}&path={{ cur_dir_path }}";
  69. var cookie= "ch_sid=" + "{{ php_session_id }}";
  70. // Check
  71. var recorder;
  72. if (!(recorder = document.getElementById("nanogongApplet"))) {
  73. alert(lang_no_applet);
  74. return
  75. }
  76. var duration = parseInt(recorder.sendGongRequest("GetMediaDuration", "audio")) || 0
  77. if (duration <= 0) {
  78. alert(lang_record_before_save);
  79. return;
  80. }
  81. var ret = recorder.sendGongRequest("PostToForm", urlNanoGong, fileInput, cookie, filename);
  82. if (ret == null) {
  83. alert(lang_failled_to_submit);
  84. } else {
  85. alert(lang_submitted);
  86. $("#status").attr('value', '1');
  87. }
  88. }
  89. function setupRecorder() {
  90. $('#wami-recorder').css('margin-bottom', '150px');
  91. Wami.setup({
  92. id : "wami",
  93. onReady : setupGUI,
  94. swfUrl : "{{ _p.web_lib }}wami-recorder/Wami.swf"
  95. });
  96. }
  97. function setupGUI() {
  98. var uniq = 'rec_' + (new Date()).getTime() + ".wav";
  99. var gui = new Wami.GUI({
  100. id : "wami-recorder",
  101. singleButton : true,
  102. recordUrl : "{{ _p.web_lib }}wami-recorder/record_document.php?lp_item_id={{ lp_item_id }}&waminame="+uniq+"&wamidir={{ cur_dir_path }}&wamiuserid={{ _u.user_id }}",
  103. buttonUrl : "{{ _p.web_lib }}wami-recorder/buttons.png",
  104. buttonNoUrl : "{{ _p.web_img }}blank.gif",
  105. onRecordStart : function() {
  106. $('#start-recording').show();
  107. },
  108. onRecordFinish: function() {
  109. $('#start-recording').hide();
  110. window.location.reload();
  111. },
  112. onError : function() {
  113. }
  114. });
  115. gui.setPlayEnabled(true);
  116. }
  117. var rtcEnabled = false;
  118. $(document).ready(function() {
  119. var isChrome = navigator.webkitGetUserMedia;
  120. $('#rtc').hide();
  121. $('#wami').hide();
  122. $('#nanogong').hide();
  123. if (rtcEnabled && isChrome) {
  124. $('#rtc').show();
  125. } else if (checkNanoGongEnable()) {
  126. $('#nanogong').show();
  127. } else if (checkWamiEnable()) {
  128. $('#wami').show();
  129. var recordWami = $('#record-wami');
  130. recordWami.on('click', function() {
  131. setupRecorder();
  132. return false;
  133. });
  134. } else {
  135. $('#record-result').html("{{ 'RecordIsNotAvailable' | get_lang }}");
  136. }
  137. // Start web RTC code
  138. var format = 'webm'; // or wav
  139. var record = $('#record');
  140. var stop = document.getElementById('stop');
  141. var preview = document.getElementById('audio');
  142. var progressInfo = document.getElementById('progress-info');
  143. var previewBlock = document.getElementById('preview');
  144. function postBlob(blob, fileType, fileName) {
  145. // FormData
  146. var formData = new FormData();
  147. formData.append(fileType + '-filename', fileName);
  148. formData.append(fileType + '-blob', blob);
  149. // progress-bar
  150. var hr = document.createElement('hr');
  151. progressInfo.appendChild(hr);
  152. var strong = document.createElement('strong');
  153. strong.innerHTML = fileType + ' upload progress: ';
  154. progressInfo.appendChild(strong);
  155. var progress = document.createElement('progress');
  156. progressInfo.appendChild(progress);
  157. // POST the Blob
  158. $.ajax({
  159. url:'{{ _p.web_ajax }}lp.ajax.php?a=record_audio&lp_item_id={{ lp_item_id }}',
  160. data: formData,
  161. processData: false,
  162. contentType: false,
  163. type: 'POST',
  164. success:function(fileURL) {
  165. window.location.reload();
  166. progressInfo.appendChild(document.createElement('hr'));
  167. var mediaElement = document.createElement(fileType);
  168. mediaElement.src = fileURL;
  169. mediaElement.controls = true;
  170. var uniq = 'id' + (new Date()).getTime();
  171. mediaElement.id = uniq;
  172. $(previewBlock).html('');
  173. previewBlock.appendChild(mediaElement);
  174. mediaElement.play();
  175. $(progressInfo).html('');
  176. window.location.reload();
  177. }
  178. });
  179. }
  180. var recordAudio, recordVideo;
  181. record.on('click', function() {
  182. record.disabled = true;
  183. var myURL = (window.URL || window.webkitURL || {});
  184. if (navigator.getUserMedia) {
  185. navigator.getUserMedia({
  186. audio: true,
  187. video: false
  188. },
  189. function(stream) {
  190. if (window.IsChrome) stream = new window.MediaStream(stream.getAudioTracks());
  191. preview.src = myURL.createObjectURL(stream);
  192. preview.play();
  193. recordAudio = RecordRTC(stream, {
  194. type: 'audio'
  195. });
  196. recordAudio.startRecording();
  197. /*recordVideo = RecordRTC(stream, {
  198. type: 'video'
  199. });*/
  200. //recordVideo.startRecording();
  201. stop.disabled = false;
  202. },
  203. function(err) {
  204. console.log("The following error occured: " + err);
  205. return false;
  206. });
  207. }
  208. });
  209. stop.onclick = function() {
  210. record.disabled = false;
  211. stop.disabled = true;
  212. var fileName = Math.round(Math.random() * 99999999) + 99999999;
  213. recordAudio.stopRecording();
  214. postBlob(recordAudio.getBlob(), 'audio', fileName + '.' + format);
  215. //recordVideo.stopRecording();
  216. //PostBlob(recordVideo.getBlob(), 'video', fileName + '.webm');
  217. preview.src = '';
  218. };
  219. // End web RTC code
  220. });
  221. </script>