Soundrecord.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <!-- -->
  2. <template>
  3. <div class="Book-record">
  4. <div class="Book-record-btn">
  5. <div
  6. :class="['playBack', hasMicro]"
  7. v-if="hasMicro"
  8. @click="playmicrophone"
  9. ></div>
  10. <div :class="['playBack', hasMicro]" v-else></div>
  11. <p>回放</p>
  12. </div>
  13. <div class="Book-record-btn">
  14. <div
  15. :class="['record', microphoneStatus ? 'active' : '']"
  16. @click="microphone"
  17. ></div>
  18. <p>{{ microphoneBtn }}</p>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. import Recorder from "js-audio-recorder"; //录音插件
  24. let Base64 = require("js-base64").Base64;
  25. export default {
  26. components: {},
  27. props: ["wavData", "queIndex"],
  28. data() {
  29. return {
  30. recorder: new Recorder({
  31. sampleBits: 16, // 采样位数,支持 8 或 16,默认是16
  32. sampleRate: 16000, // 采样率,支持 11025、16000、22050、24000、44100、48000,根据浏览器默认值,我的chrome是48000
  33. numChannels: 1, // 声道,支持 1 或 2, 默认是1
  34. }),
  35. microphoneStatus: false,
  36. hasMicro: "", // 录音后的样式class
  37. microphoneBtn: "点击录音",
  38. wavblob: null,
  39. audio: new window.Audio(),
  40. newMicrophone: false, // 是否为新录音
  41. };
  42. },
  43. computed: {},
  44. watch: {
  45. queIndex: {
  46. handler: function (val, oldVal) {
  47. let _this = this;
  48. if (val != oldVal) {
  49. _this.handleActive();
  50. }
  51. },
  52. // 深度观察监听
  53. deep: true,
  54. },
  55. },
  56. //方法集合
  57. methods: {
  58. // 开始录音
  59. microphone() {
  60. let _this = this;
  61. if (!this.microphoneStatus) {
  62. //开始录音
  63. this.recorder.start();
  64. this.microphoneStatus = true;
  65. this.microphoneBtn = "录音中...";
  66. } else {
  67. this.microphoneBtn = "录音完成";
  68. this.hasMicro = "normal";
  69. this.recorder.stop();
  70. let toltime = this.recorder.duration; //录音总时长
  71. let fileSize = this.recorder.fileSize; //录音总大小
  72. //录音结束,获取取录音数据
  73. let PCMBlob = this.recorder.getPCMBlob(); //获取 PCM 数据
  74. let wav = this.recorder.getWAVBlob(); //获取 WAV 数据
  75. this.wavblob = wav;
  76. this.microphoneStatus = false;
  77. this.newMicrophone = true;
  78. // 传值给父级
  79. // this.$emit("handle-Wav", wav);
  80. var reader = new window.FileReader();
  81. reader.readAsDataURL(wav);
  82. reader.onloadend = function () {
  83. _this.$emit("handle-Wav", reader.result);
  84. };
  85. }
  86. },
  87. playmicrophone() {
  88. let _this = this;
  89. if (!this.audio.paused) {
  90. this.audio.pause();
  91. } else {
  92. _this.audio.pause();
  93. _this.audio.load();
  94. if (_this.wavData && !_this.newMicrophone) {
  95. _this.audio.src = _this.wavData;
  96. } else {
  97. _this.audio.src = window.URL.createObjectURL(_this.wavblob);
  98. }
  99. _this.audio.play();
  100. }
  101. },
  102. // 高亮初始值
  103. handleActive() {
  104. if (this.wavData) {
  105. this.hasMicro = "normal";
  106. this.microphoneBtn = "录音完成";
  107. this.newMicrophone = false;
  108. } else {
  109. this.hasMicro = "";
  110. this.microphoneBtn = "点击录音";
  111. }
  112. },
  113. changeStatus(status) {
  114. this.hasMicro = status;
  115. },
  116. },
  117. //生命周期 - 创建完成(可以访问当前this实例)
  118. created() {
  119. this.handleActive();
  120. var that = this;
  121. window.stopAudioSound = function () {
  122. if (that.audio) {
  123. that.audio.pause();
  124. }
  125. };
  126. },
  127. //生命周期 - 挂载完成(可以访问DOM元素)
  128. mounted() {
  129. let _this = this;
  130. _this.audio.addEventListener("play", function () {
  131. _this.changeStatus("active");
  132. });
  133. _this.audio.addEventListener("pause", function () {
  134. _this.changeStatus("normal");
  135. });
  136. _this.audio.addEventListener("ended", function () {
  137. _this.changeStatus("normal");
  138. });
  139. },
  140. beforeCreate() {}, //生命周期 - 创建之前
  141. beforeMount() {}, //生命周期 - 挂载之前
  142. beforeUpdate() {}, //生命周期 - 更新之前
  143. updated() {}, //生命周期 - 更新之后
  144. beforeDestroy() {
  145. this.audio.pause();
  146. }, //生命周期 - 销毁之前
  147. destroyed() {}, //生命周期 - 销毁完成
  148. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  149. };
  150. </script>
  151. <style lang='scss' scoped>
  152. //@import url(); 引入公共css类
  153. .Book-record {
  154. display: flex;
  155. color: #ba7d21;
  156. font-size: 24px;
  157. justify-content: center;
  158. align-items: center;
  159. .Book-record-btn {
  160. display: flex;
  161. flex-direction: column;
  162. justify-content: center;
  163. align-items: center;
  164. &:nth-child(1) {
  165. margin-right: 32px;
  166. }
  167. > p {
  168. margin-top: 24px;
  169. font-weight: 600;
  170. font-family: "FZJCGFKTK";
  171. font-size: 24px;
  172. line-height: 1;
  173. text-align: center;
  174. color: #ba7d21;
  175. padding: 0;
  176. margin-bottom: 0px;
  177. }
  178. }
  179. .playBack {
  180. width: 144px;
  181. height: 72px;
  182. background: url("../../assets/qp/lookback.png") center no-repeat;
  183. background-size: 100%;
  184. cursor: not-allowed;
  185. &.normal {
  186. background: url("../../assets/qp/lookback-normal.png") center no-repeat;
  187. background-size: 100%;
  188. cursor: pointer;
  189. }
  190. &.active {
  191. background: url("../../assets/qp/lookback-click.png") center no-repeat;
  192. background-size: 100%;
  193. cursor: pointer;
  194. }
  195. }
  196. .record {
  197. width: 144px;
  198. height: 72px;
  199. background: url("../../assets/qp/luyin.png") center no-repeat;
  200. background-size: 100%;
  201. cursor: pointer;
  202. &.active {
  203. background: url("../../assets/qp/luyin-click.png") center no-repeat;
  204. background-size: 100%;
  205. }
  206. }
  207. }
  208. .questionMiddle {
  209. .Book-record {
  210. font-size: 16px;
  211. .Book-record-btn {
  212. &:nth-child(1) {
  213. margin-right: 21px;
  214. }
  215. > p {
  216. margin-top: 16px;
  217. font-size: 16px;
  218. }
  219. }
  220. .playBack {
  221. width: 96px;
  222. height: 48px;
  223. }
  224. .record {
  225. width: 96px;
  226. height: 48px;
  227. }
  228. }
  229. }
  230. .questionSmall {
  231. .Book-record {
  232. font-size: 12px;
  233. .Book-record-btn {
  234. &:nth-child(1) {
  235. margin-right: 16px;
  236. }
  237. > p {
  238. margin-top: 12px;
  239. font-size: 12px;
  240. padding: 0;
  241. margin-bottom: 0px;
  242. }
  243. }
  244. .playBack {
  245. width: 72px;
  246. height: 36px;
  247. }
  248. .record {
  249. width: 72px;
  250. height: 36px;
  251. }
  252. }
  253. }
  254. </style>