audioDemo.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <div class="home" style="margin: 1vw">
  3. <Button type="success" @click="getPermission()" style="margin: 1vw"
  4. >获取麦克风权限</Button
  5. >
  6. <br />
  7. <Button type="info" @click="startRecorder()" style="margin: 1vw"
  8. >开始录音</Button
  9. >
  10. <Button type="info" @click="resumeRecorder()" style="margin: 1vw"
  11. >继续录音</Button
  12. >
  13. <Button type="info" @click="pauseRecorder()" style="margin: 1vw"
  14. >暂停录音</Button
  15. >
  16. <Button type="info" @click="stopRecorder()" style="margin: 1vw"
  17. >结束录音</Button
  18. >
  19. <br />
  20. <Button type="success" @click="playRecorder()" style="margin: 1vw"
  21. >录音播放</Button
  22. >
  23. <Button type="success" @click="pausePlayRecorder()" style="margin: 1vw"
  24. >暂停录音播放</Button
  25. >
  26. <Button type="success" @click="resumePlayRecorder()" style="margin: 1vw"
  27. >恢复录音播放</Button
  28. >
  29. <Button type="success" @click="stopPlayRecorder()" style="margin: 1vw"
  30. >停止录音播放</Button
  31. >
  32. <br />
  33. <Button type="info" @click="getRecorder()" style="margin: 1vw"
  34. >获取录音信息</Button
  35. >
  36. <Button type="info" @click="downPCM()" style="margin: 1vw">下载PCM</Button>
  37. <Button type="info" @click="downWAV()" style="margin: 1vw">下载WAV</Button>
  38. <Button type="info" @click="getMp3Data()" style="margin: 1vw"
  39. >下载MP3</Button
  40. >
  41. <br />
  42. <Button type="error" @click="destroyRecorder()" style="margin: 1vw"
  43. >销毁录音</Button
  44. >
  45. </div>
  46. </template>
  47. <script>
  48. import Recorder from "js-audio-recorder";
  49. const lamejs = require("lamejs");
  50. const recorder = new Recorder({
  51. sampleBits: 16, // 采样位数,支持 8 或 16,默认是16
  52. sampleRate: 48000, // 采样率,支持 11025、16000、22050、24000、44100、48000,根据浏览器默认值,我的chrome是48000
  53. numChannels: 1, // 声道,支持 1 或 2, 默认是1
  54. // compiling: false,(0.x版本中生效,1.x增加中) // 是否边录边转换,默认是false
  55. });
  56. // 绑定事件-打印的是当前录音数据
  57. recorder.onprogress = function (params) {
  58. // console.log('--------------START---------------')
  59. // console.log('录音时长(秒)', params.duration);
  60. // console.log('录音大小(字节)', params.fileSize);
  61. // console.log('录音音量百分比(%)', params.vol);
  62. // console.log('当前录音的总数据([DataView, DataView...])', params.data);
  63. // console.log('--------------END---------------')
  64. };
  65. export default {
  66. name: "home",
  67. methods: {
  68. /**
  69. * 录音的具体操作功能
  70. * */
  71. // 开始录音
  72. startRecorder() {
  73. recorder.start().then(
  74. () => {
  75. },
  76. (error) => {
  77. // 出错了
  78. console.log(`${error.name} : ${error.message}`);
  79. }
  80. );
  81. },
  82. // 继续录音
  83. resumeRecorder() {
  84. recorder.resume();
  85. },
  86. // 暂停录音
  87. pauseRecorder() {
  88. recorder.pause();
  89. },
  90. // 结束录音
  91. stopRecorder() {
  92. recorder.stop();
  93. },
  94. // 录音播放
  95. playRecorder() {
  96. recorder.play();
  97. },
  98. // 暂停录音播放
  99. pausePlayRecorder() {
  100. recorder.pausePlay();
  101. },
  102. // 恢复录音播放
  103. resumePlayRecorder() {
  104. recorder.resumePlay();
  105. },
  106. // 停止录音播放
  107. stopPlayRecorder() {
  108. recorder.stopPlay();
  109. },
  110. // 销毁录音
  111. destroyRecorder() {
  112. recorder.destroy();
  113. // .then(function () {
  114. // recorder = null;
  115. // });
  116. },
  117. /**
  118. * 获取录音文件
  119. * */
  120. getRecorder() {
  121. let toltime = recorder.duration; //录音总时长
  122. let fileSize = recorder.fileSize; //录音总大小
  123. //录音结束,获取取录音数据
  124. let PCMBlob = recorder.getPCMBlob(); //获取 PCM 数据
  125. let wav = recorder.getWAVBlob(); //获取 WAV 数据
  126. let channel = recorder.getChannelData(); //获取左声道和右声道音频数据
  127. console.log(toltime);
  128. // debugger;
  129. },
  130. /**
  131. * 下载录音文件
  132. * */
  133. //下载pcm
  134. downPCM() {
  135. //这里传参进去的时文件名
  136. recorder.downloadPCM("新文件");
  137. },
  138. //下载wav
  139. downWAV() {
  140. //这里传参进去的时文件名
  141. recorder.downloadWAV("新文件");
  142. },
  143. /**
  144. * 获取麦克风权限
  145. * */
  146. getPermission() {
  147. Recorder.getPermission().then(
  148. () => {
  149. this.$Message.success("获取权限成功");
  150. },
  151. (error) => {
  152. console.log(`${error.name} : ${error.message}`);
  153. }
  154. );
  155. },
  156. /**
  157. * 文件格式转换 wav-map3
  158. * */
  159. getMp3Data() {
  160. const mp3Blob = this.convertToMp3(recorder.getWAV());
  161. recorder.download(mp3Blob, "recorder", "mp3");
  162. },
  163. convertToMp3(wavDataView) {
  164. // 获取wav头信息
  165. const wav = lamejs.WavHeader.readHeader(wavDataView); // 此处其实可以不用去读wav头信息,毕竟有对应的config配置
  166. const { channels, sampleRate } = wav;
  167. const mp3enc = new lamejs.Mp3Encoder(channels, sampleRate, 128);
  168. // 获取左右通道数据
  169. const result = recorder.getChannelData();
  170. const buffer = [];
  171. const leftData =
  172. result.left &&
  173. new Int16Array(result.left.buffer, 0, result.left.byteLength / 2);
  174. const rightData =
  175. result.right &&
  176. new Int16Array(result.right.buffer, 0, result.right.byteLength / 2);
  177. const remaining = leftData.length + (rightData ? rightData.length : 0);
  178. const maxSamples = 1152;
  179. for (let i = 0; i < remaining; i += maxSamples) {
  180. const left = leftData.subarray(i, i + maxSamples);
  181. let right = null;
  182. let mp3buf = null;
  183. if (channels === 2) {
  184. right = rightData.subarray(i, i + maxSamples);
  185. mp3buf = mp3enc.encodeBuffer(left, right);
  186. } else {
  187. mp3buf = mp3enc.encodeBuffer(left);
  188. }
  189. if (mp3buf.length > 0) {
  190. buffer.push(mp3buf);
  191. }
  192. }
  193. const enc = mp3enc.flush();
  194. if (enc.length > 0) {
  195. buffer.push(enc);
  196. }
  197. return new Blob(buffer, { type: "audio/mp3" });
  198. },
  199. },
  200. };
  201. </script>
  202. <style scoped>
  203. </style>