ソースを参照

语音录制功能修改

zhangjie 4 年 前
コミット
391ddddcdb

+ 0 - 1
src/features/invigilation/RealtimeMonitoring/audioRecord/AudioRecordDialog.vue

@@ -148,7 +148,6 @@ export default {
         this.$refs.AudioAudio.pause();
         this.isPlaying = false;
       }
-      console.dir(this.$refs.AudioAudio);
     },
     audioCurrentTimeChange() {
       const { duration, currentTime } = this.$refs.AudioAudio;

+ 21 - 4
src/features/invigilation/RealtimeMonitoring/audioRecord/audioRecord.js

@@ -2,7 +2,7 @@ class AudioRecord {
   constructor(opt = {}) {
     const defaultOptions = {
       sampleBits: 8,
-      sampleRate: 16000 / 6,
+      sampleRate: 16000,
       onaudioprocess: function () {},
     };
     this.options = Object.assign(defaultOptions, opt);
@@ -17,6 +17,7 @@ class AudioRecord {
     this.audioContext = null;
     this.media = null;
     this.recorder = null;
+    this.stream = null;
   }
 
   input(data) {
@@ -115,8 +116,8 @@ class AudioRecord {
     }
     return new File(
       [audioDataView],
-      Math.random().toString().slice(-8) + ".mp3",
-      { type: "audio/mpeg3" }
+      Math.random().toString().slice(-8) + ".wav",
+      { type: "audio/wav" }
     );
     // return new Blob([audioDataView], { type: "audio/mpeg3" });
   }
@@ -154,9 +155,9 @@ class AudioRecord {
       (stream) => {
         this.audioContext = new window.AudioContext();
         this.inputSampleRate = this.audioContext.sampleRate;
-        console.log(this.inputSampleRate);
         this.media = this.audioContext.createMediaStreamSource(stream);
         this.recorder = this.audioContext.createScriptProcessor(4096, 1, 1);
+        this.stream = stream;
         //音频采集
         this.recorder.onaudioprocess = (e) => {
           this.input(e.inputBuffer.getChannelData(0));
@@ -186,11 +187,27 @@ class AudioRecord {
   }
 
   recover() {
+    this.destroy();
     this.size = 0;
     this.audioBuffer = [];
     this.audioContext = null;
     this.media = null;
     this.recorder = null;
+    this.stream = null;
+  }
+
+  destroy() {
+    if (this.stream && this.stream.getTracks) {
+      this.stream.getTracks().forEach((track) => track.stop());
+      this.stream = null;
+    }
+    if (
+      this.audioContext &&
+      this.audioContext.close &&
+      this.audioContext.state !== "closed"
+    ) {
+      return this.audioContext.close();
+    }
   }
 }