record_voice.tpl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <div id="record-audio-recordrtc" class="text-center">
  2. <p>
  3. <span class="fa fa-microphone fa-5x fa-fw" aria-hidden="true"></span>
  4. <span class="sr-only">{{ 'RecordAudio'|get_lang }}</span>
  5. </p>
  6. <button class="btn btn-primary" type="button" id="btn-start-record">
  7. <span class="fa fa-circle fa-fw" aria-hidden="true"></span> {{ 'StartRecordingAudio'|get_lang }}
  8. </button>
  9. <button class="btn btn-success" type="button" id="btn-stop-record" disabled>
  10. <span class="fa fa-square fa-fw" aria-hidden="true"></span> {{ 'StopRecordingAudio'|get_lang }}
  11. </button>
  12. </div>
  13. <div id="record-audio-wami" class="wami-container"></div>
  14. <script>
  15. $(document).on('ready', function () {
  16. function useRecordRTC(){
  17. $('#record-audio-recordrtc').show();
  18. var mediaConstraints = {audio: true},
  19. recordRTC = null,
  20. btnStart = $('#btn-start-record'),
  21. btnStop = $('#btn-stop-record');
  22. btnStart.on('click', function () {
  23. navigator.getUserMedia = navigator.getUserMedia ||
  24. navigator.mozGetUserMedia ||
  25. navigator.webkitGetUserMedia;
  26. if (navigator.getUserMedia) {
  27. navigator.getUserMedia(mediaConstraints, successCallback, errorCallback);
  28. } else if (navigator.mediaDevices.getUserMedia) {
  29. navigator.mediaDevices.getUserMedia(mediaConstraints)
  30. .then(successCallback).error(errorCallback);
  31. }
  32. function successCallback(stream) {
  33. recordRTC = RecordRTC(stream, {
  34. numberOfAudioChannels: 1,
  35. type: 'audio'
  36. });
  37. recordRTC.startRecording();
  38. btnStop.prop('disabled', false);
  39. btnStart.prop('disabled', true);
  40. }
  41. function errorCallback(error) {
  42. alert(error.message);
  43. }
  44. });
  45. btnStop.on('click', function () {
  46. if (!recordRTC) {
  47. return;
  48. }
  49. recordRTC.stopRecording(function (audioURL) {
  50. var recordedBlob = recordRTC.getBlob(),
  51. fileName = Math.round(Math.random() * 99999999) + 99999999,
  52. fileExtension = '.' + recordedBlob.type.split('/')[1];
  53. var formData = new FormData();
  54. formData.append('audio-filename', fileName + fileExtension);
  55. formData.append('audio-blob', recordedBlob, 'audio' + fileExtension);
  56. $.ajax({
  57. url: '{{ _p.web_ajax }}lp.ajax.php?a=record_audio&lp_item_id={{ lp_item_id }}',
  58. data: formData,
  59. processData: false,
  60. contentType: false,
  61. type: 'POST',
  62. success: function (fileURL) {
  63. if (!fileURL) {
  64. return;
  65. }
  66. window.location.reload();
  67. }
  68. });
  69. btnStop.prop('disabled', true);
  70. btnStart.prop('disabled', false);
  71. });
  72. });
  73. }
  74. function useWami(){
  75. $('#record-audio-wami').show();
  76. function setupGUI() {
  77. var gui = new Wami.GUI({
  78. id : 'record-audio-wami',
  79. singleButton : true,
  80. recordUrl : '{{ _p.web_ajax }}record_audio_wami.ajax.php?' + $.param({
  81. waminame: 'rec_' + (new Date()).getTime() + '.wav',
  82. wamidir: '{{ cur_dir_path }}',
  83. wamiuserid: {{ _u.user_id }},
  84. lp_item_id: {{ lp_item_id }}
  85. }),
  86. buttonUrl : '{{ _p.web_lib }}wami-recorder/buttons.png',
  87. buttonNoUrl: '{{ _p.web_img }}blank.gif',
  88. onRecordFinish: function() {
  89. $('#start-recording').hide();
  90. window.location.reload();
  91. }
  92. });
  93. gui.setPlayEnabled(false);
  94. }
  95. Wami.setup({
  96. id : "record-audio-wami",
  97. onReady : setupGUI,
  98. swfUrl: '{{ _p.web_lib }}wami-recorder/Wami.swf'
  99. });
  100. }
  101. $('#record-audio-recordrtc, #record-audio-wami').hide();
  102. var webRTCIsEnabled = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia ||
  103. navigator.mediaDevices.getUserMedia;
  104. if (webRTCIsEnabled) {
  105. useRecordRTC();
  106. return;
  107. }
  108. useWami();
  109. });
  110. </script>