Selaa lähdekoodia

录音组件修复

dsy 1 kuukausi sitten
vanhempi
commit
7b308c0489

+ 1 - 1
packages/BookEep.vue

@@ -417,7 +417,7 @@ export default {
      * @returns {Promise<{ ok: boolean, timedOut: boolean, failed: number }>}
      */
     async waitForPendingRecordings(options) {
-      stopAllRecorders();
+      await stopAllRecorders();
       await this.$nextTick();
       return drainUploads(options);
     },

+ 13 - 10
src/utils/recordingUpload.js

@@ -63,7 +63,7 @@ export function trackUpload(promise) {
 
 /**
  * 登记活跃录音器
- * @param {{ stopIfRecording: Function }} api
+ * @param {{ stopIfRecording: () => (void|Promise<void>) }} api
  * @returns {Function} 注销函数
  */
 export function registerRecorder(api) {
@@ -73,16 +73,19 @@ export function registerRecorder(api) {
 
 /**
  * 停止所有仍在录音的录音器(停止动作本身会触发上传)
+ * @returns {Promise<void>} 全部录音器完成停止流程后 resolve
  */
-export function stopAllRecorders() {
-  activeRecorders.forEach((api) => {
-    try {
-      api.stopIfRecording();
-    } catch (err) {
-      // 单个录音器异常不影响其它录音器
-      console.warn('[audio] 停止录音失败:', (err && err.message) || err);
-    }
-  });
+export async function stopAllRecorders() {
+  await Promise.all(
+    [...activeRecorders].map(async (api) => {
+      try {
+        await api.stopIfRecording();
+      } catch (err) {
+        // 单个录音器异常不影响其它录音器
+        console.warn('[audio] 停止录音失败:', (err && err.message) || err);
+      }
+    }),
+  );
 }
 
 /**

+ 18 - 20
src/views/book/courseware/preview/common/SoundRecord.vue

@@ -305,8 +305,6 @@ export default {
      * 获取录音器(按需创建,直接挂在实例上,不做成 data,避免 Vue 深度观测录音器内部的大数组)。
      * js-audio-recorder 每次实例化都会创建一个 AudioContext(仅用于读取输入采样率且不会释放),
      * 而页面里每个跟读组件都会实例化一次,课件较多时会耗尽浏览器音频输出流上限,
-     * 控制台报 “The AudioContext encountered an error from the audio device or the
-     * WebAudio renderer.”(详见 utils/releaseRecorder 注释)。
      */
     getRecorder() {
       if (!this.recorderInstance) {
@@ -329,11 +327,7 @@ export default {
       }
     },
     /**
-     * 开始录音。
-     * start() 内部要先走 getUserMedia,成功后才真正建立音频链路;
-     * 这段时间里用户可能马上再点一次停止,若此时就销毁录音器,
-     * getUserMedia 的回调会拿不到可用的 AudioContext(麦克风流不会被释放,下一次 start() 也可能失败),
-     * 表现为“有时录不了下一条”。所以把启动过程记录下来,停止时先等它结束。
+     * 开始录音
      */
     startRecording() {
       this.hasMicro = '';
@@ -422,10 +416,20 @@ export default {
      * 录音先以 dataURL 写回答案(结构完整、可立即试听),上传成功后再替换为 file_id,
      * 避免答案里内联 MB 级 base64 导致提交请求超时。
      */
-    async handleStopRecording() {
-      // 连点时防止重复走停止流程(会重复上传、重复写回答案)
-      if (this.stopping) return;
-      this.stopping = true;
+    handleStopRecording() {
+      if (this.stopPromise) return this.stopPromise;
+      const stopPromise = this.finishStopRecording().finally(() => {
+        if (this.stopPromise === stopPromise) this.stopPromise = null;
+        if (this.microphoneStatus) {
+          this.microphoneStatus = false;
+          this.$root.isRecording = false;
+          clearInterval(this.timer);
+        }
+      });
+      this.stopPromise = stopPromise;
+      return stopPromise;
+    },
+    async finishStopRecording() {
       const recorder = this.getRecorder();
       try {
         const started = await this.waitRecordStart();
@@ -512,19 +516,13 @@ export default {
         console.warn('[audio] 录音保存失败:', (err && err.message) || err);
         this.$message.error('录音保存失败,请重新录制');
         this.removeRecording(this.recordingId);
-      } finally {
-        this.stopping = false;
-        // 异常时也要复位,否则按钮会一直停在“录音中”,后面再也录不了
-        if (this.microphoneStatus) {
-          this.microphoneStatus = false;
-          this.$root.isRecording = false;
-          clearInterval(this.timer);
-        }
       }
     },
     // 供提交前统一停止:仍在录音则先停止(停止会触发上传)
     stopIfRecording() {
-      if (this.microphoneStatus) this.microphone();
+      if (this.stopPromise) return this.stopPromise;
+      if (this.microphoneStatus) return this.handleStopRecording();
+      return Promise.resolve();
     },
     async playmicrophone(totalTimes) {
       if (this.hasMicro) {