audioDemo.vue 6.3 KB

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