record_audio.tpl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-groups">
  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-success" 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. </div>
  26. </div>
  27. </form>
  28. </div>
  29. <div class="row" id="record-audio-wami">
  30. <div class="col-sm-3 col-sm-offset-3">
  31. <br>
  32. <form>
  33. <div class="form-group">
  34. <input type="text" name="audio_title" id="audio-title-wami" class="form-control" placeholder="{{ 'InputNameHere'|get_lang }}">
  35. </div>
  36. <div class="form-group text-center">
  37. <button class="btn btn-primary" type="button" id="btn-activate-wami">
  38. <span class="fa fa-check fa-fw" aria-hidden=""></span> {{ 'Activate'|get_lang }}
  39. </button>
  40. </div>
  41. </form>
  42. </div>
  43. <div class="col-sm-3">
  44. <div id="record-audio-wami-container" class="wami-container"></div>
  45. </div>
  46. </div>
  47. <script>
  48. $(document).on('ready', function () {
  49. function useRecordRTC() {
  50. $('#record-audio-recordrtc').show();
  51. var audioTitle = '';
  52. var mediaConstraints = {audio: true},
  53. recordRTC = null,
  54. btnStart = $('#btn-start-record'),
  55. btnStop = $('#btn-stop-record');
  56. btnStart.on('click', function () {
  57. audioTitle = $('#audio-title-rtc').val();
  58. if (!$.trim(audioTitle)) {
  59. return;
  60. }
  61. navigator.getUserMedia = navigator.getUserMedia ||
  62. navigator.mozGetUserMedia ||
  63. navigator.webkitGetUserMedia;
  64. if (navigator.getUserMedia) {
  65. navigator.getUserMedia(mediaConstraints, successCallback, errorCallback);
  66. } else if (navigator.mediaDevices.getUserMedia) {
  67. navigator.mediaDevices.getUserMedia(mediaConstraints)
  68. .then(successCallback).error(errorCallback);
  69. }
  70. function successCallback(stream) {
  71. recordRTC = RecordRTC(stream, {
  72. numberOfAudioChannels: 1,
  73. type: 'audio'
  74. });
  75. recordRTC.startRecording();
  76. $('#audio-title-rtc').prop('readonly', true);
  77. btnStop.prop('disabled', false);
  78. btnStart.prop('disabled', true);
  79. }
  80. function errorCallback(error) {
  81. alert(error.message);
  82. }
  83. });
  84. btnStop.on('click', function () {
  85. if (!recordRTC) {
  86. return;
  87. }
  88. recordRTC.stopRecording(function (audioURL) {
  89. var recordedBlob = recordRTC.getBlob(),
  90. fileName = audioTitle,
  91. fileExtension = '.' + recordedBlob.type.split('/')[1];
  92. var formData = new FormData();
  93. formData.append('audio_blob', recordedBlob, audioTitle + fileExtension);
  94. formData.append('audio_dir', '{{ directory }}');
  95. $.ajax({
  96. url: '{{ _p.web_ajax }}record_audio_rtc.ajax.php',
  97. data: formData,
  98. processData: false,
  99. contentType: false,
  100. type: 'POST',
  101. success: function (fileURL) {
  102. if (!fileURL) {
  103. return;
  104. }
  105. window.location.reload();
  106. }
  107. });
  108. $('#audio-title-rtc').prop('readonly', false);
  109. btnStop.prop('disabled', true);
  110. btnStart.prop('disabled', false);
  111. });
  112. });
  113. }
  114. function useWami() {
  115. $('#record-audio-wami').show();
  116. var audioTitle = '';
  117. $('#btn-activate-wami').on('click', function (e) {
  118. e.preventDefault();
  119. audioTitle = $('#audio-title-wami').val();
  120. if (!$.trim(audioTitle)) {
  121. return;
  122. }
  123. $('#audio-title-wami').prop('readonly', true);
  124. $(this).prop('disabled', true);
  125. Wami.setup({
  126. id : "record-audio-wami-container",
  127. onReady : setupGUI,
  128. swfUrl: '{{ _p.web_lib }}wami-recorder/Wami.swf'
  129. });
  130. });
  131. function setupGUI() {
  132. var gui = new Wami.GUI({
  133. id : 'record-audio-wami-container',
  134. singleButton : true,
  135. recordUrl : '{{ _p.web_ajax }}record_audio_wami.ajax.php?' + $.param({
  136. waminame: audioTitle + '.wav',
  137. wamidir: '{{ directory }}',
  138. wamiuserid: {{ user_id }}
  139. }),
  140. buttonUrl : '{{ _p.web_lib }}wami-recorder/buttons.png',
  141. buttonNoUrl: '{{ _p.web_img }}blank.gif',
  142. onRecordFinish: function() {
  143. window.location.reload();
  144. }
  145. });
  146. gui.setPlayEnabled(false);
  147. }
  148. }
  149. $('#record-audio-recordrtc, #record-audio-wami').hide();
  150. var webRTCIsEnabled = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia ||
  151. navigator.mediaDevices.getUserMedia;
  152. if (webRTCIsEnabled) {
  153. useRecordRTC();
  154. return;
  155. }
  156. useWami();
  157. });
  158. </script>