record_audio.tpl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <div class="alert alert-warning">
  2. <span class="fa fa-warning fa-fw" aria-hidden="true"></span> {{ 'WamiNeedFilename'|get_lang }}
  3. </div>
  4. <div id="record-audio-recordrtc" class="row text-center">
  5. <form>
  6. <div class="row">
  7. <div class="col-sm-4 col-sm-offset-4">
  8. <div class="form-group">
  9. <span class="fa fa-microphone fa-5x fa-fw" aria-hidden="true"></span>
  10. <span class="sr-only">{{ 'RecordAudio'|get_lang }}</span>
  11. </div>
  12. <div class="form-group">
  13. <input type="text" name="audio_title" id="audio-title-rtc" class="form-control" placeholder="{{ 'InputNameHere'|get_lang }}">
  14. </div>
  15. </div>
  16. </div>
  17. <div class="text-center">
  18. <div class="form-group">
  19. <button class="btn btn-primary" type="button" id="btn-start-record">
  20. <span class="fa fa-circle fa-fw" aria-hidden="true"></span> {{ 'StartRecordingAudio'|get_lang }}
  21. </button>
  22. <button class="btn btn-danger" type="button" id="btn-stop-record" disabled>
  23. <span class="fa fa-square fa-fw" aria-hidden="true"></span> {{ 'StopRecordingAudio'|get_lang }}
  24. </button>
  25. <button class="btn btn-success" type="button" id="btn-save-record" disabled>
  26. <span class="fa fa-send fa-fw" aria-hidden="true"></span> {{ 'SaveRecordedAudio'|get_lang }}
  27. </button>
  28. </div>
  29. <div class="form-group">
  30. <audio class="skip hidden center-block" controls id="record-preview"></audio>
  31. </div>
  32. </div>
  33. </form>
  34. </div>
  35. <div class="row" id="record-audio-wami">
  36. <div class="col-sm-3 col-sm-offset-3">
  37. <br>
  38. <form>
  39. <div class="form-group">
  40. <input type="text" name="audio_title" id="audio-title-wami" class="form-control" placeholder="{{ 'InputNameHere'|get_lang }}">
  41. </div>
  42. <div class="form-group text-center">
  43. <button class="btn btn-primary" type="button" id="btn-activate-wami">
  44. <span class="fa fa-check fa-fw" aria-hidden=""></span> {{ 'Activate'|get_lang }}
  45. </button>
  46. </div>
  47. </form>
  48. </div>
  49. <div class="col-sm-3">
  50. <div id="record-audio-wami-container" class="wami-container"></div>
  51. </div>
  52. </div>
  53. <script>
  54. $(document).on('ready', function() {
  55. function useRecordRTC() {
  56. $('#record-audio-recordrtc').show();
  57. var audioTitle = '';
  58. var mediaConstraints = {audio: true},
  59. recordRTC = null,
  60. btnStart = $('#btn-start-record'),
  61. btnStop = $('#btn-stop-record'),
  62. btnSave = $('#btn-save-record'),
  63. tagAudio = $('#record-preview');
  64. btnStart.on('click', function () {
  65. audioTitle = $('#audio-title-rtc').val();
  66. if (!$.trim(audioTitle)) {
  67. return;
  68. }
  69. navigator.getUserMedia = navigator.getUserMedia ||
  70. navigator.mozGetUserMedia ||
  71. navigator.webkitGetUserMedia;
  72. if (navigator.getUserMedia) {
  73. navigator.getUserMedia(mediaConstraints, successCallback, errorCallback);
  74. } else if (navigator.mediaDevices.getUserMedia) {
  75. navigator.mediaDevices.getUserMedia(mediaConstraints)
  76. .then(successCallback).error(errorCallback);
  77. }
  78. function successCallback(stream) {
  79. recordRTC = RecordRTC(stream, {
  80. numberOfAudioChannels: 1,
  81. type: 'audio'
  82. });
  83. recordRTC.startRecording();
  84. $('#audio-title-rtc').prop('readonly', true);
  85. btnSave.prop('disabled', true);
  86. btnStop.prop('disabled', false);
  87. btnStart.prop('disabled', true);
  88. tagAudio.removeClass('show').addClass('hidden');
  89. }
  90. function errorCallback(error) {
  91. alert(error.message);
  92. }
  93. });
  94. btnStop.on('click', function () {
  95. if (!recordRTC) {
  96. return;
  97. }
  98. recordRTC.stopRecording(function (audioURL) {
  99. btnStart.prop('disabled', false);
  100. btnStop.prop('disabled', true);
  101. btnSave.prop('disabled', false);
  102. tagAudio
  103. .removeClass('hidden')
  104. .addClass('show')
  105. .prop('src', audioURL);
  106. });
  107. });
  108. btnSave.on('click', function () {
  109. if (!recordRTC) {
  110. return;
  111. }
  112. var recordedBlob = recordRTC.getBlob();
  113. if (!recordedBlob) {
  114. return;
  115. }
  116. var fileName = audioTitle,
  117. fileExtension = '.' + recordedBlob.type.split('/')[1];
  118. var formData = new FormData();
  119. formData.append('audio_blob', recordedBlob, audioTitle + fileExtension);
  120. formData.append('audio_dir', '{{ directory }}');
  121. $.ajax({
  122. url: '{{ _p.web_ajax }}record_audio_rtc.ajax.php',
  123. data: formData,
  124. processData: false,
  125. contentType: false,
  126. type: 'POST',
  127. success: function (fileURL) {
  128. if (!fileURL) {
  129. return;
  130. }
  131. $('#audio-title-rtc').prop('readonly', false);
  132. btnSave.prop('disabled', true);
  133. btnStop.prop('disabled', true);
  134. btnStart.prop('disabled', false);
  135. window.location.reload();
  136. }
  137. });
  138. });
  139. }
  140. function useWami() {
  141. $('#record-audio-wami').show();
  142. var audioTitle = '';
  143. $('#btn-activate-wami').on('click', function (e) {
  144. e.preventDefault();
  145. audioTitle = $('#audio-title-wami').val();
  146. if (!$.trim(audioTitle)) {
  147. return;
  148. }
  149. $('#audio-title-wami').prop('readonly', true);
  150. $(this).prop('disabled', true);
  151. Wami.setup({
  152. id : "record-audio-wami-container",
  153. onReady : setupGUI,
  154. swfUrl: '{{ _p.web_lib }}wami-recorder/Wami.swf'
  155. });
  156. });
  157. function setupGUI() {
  158. var gui = new Wami.GUI({
  159. id : 'record-audio-wami-container',
  160. singleButton : true,
  161. recordUrl : '{{ _p.web_ajax }}record_audio_wami.ajax.php?' + $.param({
  162. waminame: audioTitle + '.wav',
  163. wamidir: '{{ directory }}',
  164. wamiuserid: {{ user_id }}
  165. }),
  166. buttonUrl : '{{ _p.web_lib }}wami-recorder/buttons.png',
  167. buttonNoUrl: '{{ _p.web_img }}blank.gif',
  168. onRecordFinish: function() {
  169. window.location.reload();
  170. }
  171. });
  172. gui.setPlayEnabled(false);
  173. }
  174. }
  175. $('#record-audio-recordrtc, #record-audio-wami').hide();
  176. var webRTCIsEnabled = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia ||
  177. navigator.mediaDevices.getUserMedia;
  178. if (webRTCIsEnabled) {
  179. useRecordRTC();
  180. return;
  181. }
  182. useWami();
  183. });
  184. </script>