Explorar el Código

生词录音调整

natasha hace 1 semana
padre
commit
8e34557fd0

+ 131 - 16
src/views/book/courseware/preview/common/RecordScoreWholeBtn.vue

@@ -59,13 +59,19 @@
       <SvgIcon icon-class="luyin-delete" size="24" />
     </a>
     <el-button
+      v-if="showScoring"
       type="text"
-      :class="['record-score-btn', wholeRecordSelectList[selectWholeIndex] ? 'record-score-btn-has' : '']"
+      :class="[
+        'record-score-btn',
+        wholeRecordSelectList[selectWholeIndex] && wholeRecordSelectList[selectWholeIndex].isScore
+          ? 'record-score-btn-has'
+          : '',
+      ]"
       @click="handleWholeScore()"
       >评分</el-button
     >
     <RecordProgressBar
-      v-if="microWholeStatus"
+      v-if="microWholeStatus && showScoring"
       :index="luyinIndex"
       :total="totalIndex"
       :unit="recordType === 'sentence' ? '句' : '词'"
@@ -167,6 +173,7 @@ export default {
       type: Array,
       default: () => [],
     },
+    showScoring: { type: Boolean, default: true },
   },
   data() {
     return {
@@ -232,6 +239,7 @@ export default {
       this.isPlaying = false;
       this.recordtime = this.wholeRecordSelectList[this.selectWholeIndex].toltime;
       this.playtime = 0;
+      clearInterval(this.timer);
     });
     // 登记录音器,供提交前统一停止(停止会触发录音上传)
     this.unregisterRecorder = registerRecorder({ stopIfRecording: () => this.stopIfRecording() });
@@ -261,21 +269,28 @@ export default {
     // 点击开始录音按钮
     changeMicroStatus() {
       this.microWholeStatus = !this.microWholeStatus;
-      this.$emit('changeWholeStatus', this.microWholeStatus, 0);
-      this.luyinIndex = 0;
-      this.handleDestroyRecord();
-      this.$nextTick(() => {
-        if (this.microWholeStatus) {
-          this.recordList = new Array(this.totalIndex).fill();
-          this.microphone();
-        }
-      });
+      if (this.showScoring) {
+        // 整体评分录音模式
+        this.$emit('changeWholeStatus', this.microWholeStatus, 0);
+        this.luyinIndex = 0;
+        this.handleDestroyRecord();
+        this.$nextTick(() => {
+          if (this.microWholeStatus) {
+            this.recordList = new Array(this.totalIndex).fill();
+            this.microphone();
+          }
+        });
+      } else {
+        // 普通录音模式
+        this.microphone();
+      }
     },
     // 重置销毁录音
     handleDestroyRecord() {
       if (this.timer) clearInterval(this.timer);
       this.$root.isRecording = false;
       this.microphoneStatus = false;
+      this.$emit('changeIsRecord', this.microphoneStatus);
       this.recordStartPromise = null;
       this.recordtime = 0;
       this.hasMicro = '';
@@ -303,12 +318,14 @@ export default {
     // 点击下一个按钮
     handleNext(type) {
       if (type === 'finish') {
+        this.isAllDone = false;
         this.handleStopRecording();
         this.microWholeStatus = false;
         this.wholeRecordSelectList.push({
           name: `整体录音${this.wholeRecordSelectList.length + 1}`,
           src: '',
           toltime: 0,
+          isScore: true,
         });
         this.selectWholeIndex = this.wholeRecordSelectList.length - 1;
         this.timers = setInterval(() => {
@@ -370,10 +387,29 @@ export default {
     // 开始录音
     microphone() {
       if (!this.taskModel || this.taskModel !== 'ANSWER') {
-        if (this.microphoneStatus) {
-          this.handleStopRecording();
+        if (this.showScoring) {
+          // 整体录音评分模式
+          if (this.microphoneStatus) {
+            this.handleStopRecording();
+          } else {
+            this.startRecording();
+          }
         } else {
-          this.startRecording();
+          if (this.microphoneStatus) {
+            this.handleStopRecordingNormal();
+            this.wholeRecordSelectList.push({
+              name: `录音${this.wholeRecordSelectList.length + 1}`,
+              src: '',
+              toltime: 0,
+              isScore: false,
+            });
+            this.selectWholeIndex = this.wholeRecordSelectList.length - 1;
+
+            this.$emit('updataWholeRecordList', this.recordList);
+            this.$emit('getWholeRecordIndex', this.wholeRecordSelectList.length - 1);
+          } else {
+            this.startRecordingNormal();
+          }
         }
       }
     },
@@ -395,6 +431,7 @@ export default {
         });
       }
       this.microphoneStatus = true;
+      this.$emit('changeIsRecord', this.microphoneStatus);
       this.recordtime = 0;
       this.isPlaying = false;
       clearInterval(this.timer);
@@ -450,6 +487,7 @@ export default {
       clearInterval(this.timer);
       this.$root.isRecording = false;
       this.microphoneStatus = false;
+      this.$emit('changeIsRecord', this.microphoneStatus);
       this.recordStartPromise = null;
       this.recordtime = 0;
       this.hasMicro = '';
@@ -481,6 +519,7 @@ export default {
         if (this.stopPromise === stopPromise) this.stopPromise = null;
         if (this.microphoneStatus) {
           this.microphoneStatus = false;
+          this.$emit('changeIsRecord', this.microphoneStatus);
           this.$root.isRecording = false;
           clearInterval(this.timer);
         }
@@ -504,6 +543,7 @@ export default {
         // 录音结束立即释放录音占用的 AudioContext 与麦克风流,避免多次录音不断累积
         releaseRecorder(recorder);
         this.microphoneStatus = false;
+        this.$emit('changeIsRecord', this.microphoneStatus);
         this.hasMicro = 'normal';
 
         const index = this.selectIndex;
@@ -554,6 +594,17 @@ export default {
           .then((res) => {
             patchTarget({ wavData: res.file_id, file_url: res.file_url || '', upload_state: 'done' });
             if (this.selectIndex === index) this.wavblob = res.file_id;
+            if (!this.showScoring) {
+              this.$nextTick(() => {
+                this.wholeRecordSelectList[this.selectWholeIndex].src =
+                  this.recordList[this.recordList.length - 1].file_url;
+                this.wholeRecordSelectList[this.selectWholeIndex].audio_file_id =
+                  this.recordList[this.recordList.length - 1].wavData;
+                this.wholeRecordSelectList[this.selectWholeIndex].toltime =
+                  this.recordList[this.recordList.length - 1].toltime;
+                this.recordtime = this.recordList[this.recordList.length - 1].toltime;
+              });
+            }
             emitAnswer();
           })
           // 上传失败:保留 dataURL 并标记状态,由提交前统一决策
@@ -781,6 +832,70 @@ export default {
       // this.curQue.Bookanswer.practiceModel[this.sentIndex].scoreList[this.selectIndex] = scoreObj;
       return scoreObj;
     },
+
+    /**
+     * 开始录音
+     */
+    startRecordingNormal() {
+      this.hasMicro = '';
+      this.$root.isRecording = true;
+      this.$emit('getWavblob', null);
+      this.$emit('getSelectData', { type: '' });
+      // 开始录音
+      const recorder = this.getRecorder();
+      const startPromise = recorder.start();
+      this.recordStartPromise = startPromise;
+      if (startPromise && typeof startPromise.catch === 'function') {
+        startPromise.catch((err) => {
+          if (this.recordStartPromise === startPromise) this.handleRecordStartFail(err);
+        });
+      }
+      this.microphoneStatus = true;
+      this.$emit('changeIsRecord', this.microphoneStatus);
+      this.recordtime = 0;
+      this.isPlaying = false;
+      clearInterval(this.timer);
+      this.timer = setInterval(() => {
+        if (this.recordtime < 600) {
+          this.recordtime += 1;
+        } else {
+          // 与创作端保持一致的单条录音上限,避免产生超大录音
+          this.$message.warning('单条记录最多可以录音10分钟');
+          this.microphone();
+        }
+      }, 1000);
+      this.$emit('handleParentPlay');
+      let obj = {
+        name: this.fileName ? this.fileName + this.recordFile : `新录音${this.recordFile}`,
+        id: (this.recordFile + Math.random().toString(36).substring(2, 12)).toString(),
+      };
+      if (this.selectData) obj.selectData = this.selectData;
+      this.recordList.push(obj);
+      this.recordFile += 1;
+      this.selectIndex = this.recordList.length - 1;
+      // 记住本次录音对应哪一条,停止时按 id 回写,避免列表被父组件回灌后写到别的条目上
+      this.recordingId = obj.id;
+      this.$emit('getRecordIndex', this.selectIndex);
+    },
+    /**
+     * 停止录音并把录音上传到文件服务器。
+     * 录音先以 dataURL 写回答案(结构完整、可立即试听),上传成功后再替换为 file_id,
+     * 避免答案里内联 MB 级 base64 导致提交请求超时。
+     */
+    handleStopRecordingNormal() {
+      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.$emit('changeIsRecord', this.microphoneStatus);
+          this.$root.isRecording = false;
+          clearInterval(this.timer);
+        }
+      });
+      this.stopPromise = stopPromise;
+      return stopPromise;
+    },
   },
 };
 /* istanbul ignore next */ /* c8 ignore start */ /* eslint-disable */ function oo_cm() {
@@ -937,11 +1052,11 @@ export default {
   .record-score-btn {
     padding: 0 9px;
     font-weight: bold;
-    color: #3b6fd4;
+    color: #c0c4cc;
     cursor: not-allowed;
-    opacity: 0.35;
 
     &-has {
+      color: #3b6fd4;
       cursor: pointer;
       opacity: 1;
     }

+ 36 - 5
src/views/book/courseware/preview/components/new_word/NewWordPreview.vue

@@ -51,7 +51,7 @@
                     : 'rgba(0,0,0,0.65)',
               }"
               :title="convertText('语音测评')"
-              @click.stop="assessmentMode = !assessmentMode"
+              @click.stop="toggleAssessmentMode"
               v-if="
                 !(data.property.is_enable_score && data.property.is_enable_score === 'false') &&
                 !data.audio_data.file_id
@@ -341,7 +341,7 @@
                         : 'rgba(0,0,0,0.65)',
                   }"
                   :title="convertText('语音测评')"
-                  @click.stop="assessmentMode = !assessmentMode"
+                  @click.stop="toggleAssessmentMode"
                   v-if="!(data.property.is_enable_score && data.property.is_enable_score === 'false')"
                 >
                   <svg-icon icon-class="icon-article-practice" size="24" />
@@ -820,7 +820,11 @@
                           >评分</el-button
                         >
                         <template
-                          v-if="showRecordScoreList[sItem.sIndex] && showRecordScoreList[sItem.sIndex].detail_list"
+                          v-if="
+                            assessmentMode &&
+                            showRecordScoreList[sItem.sIndex] &&
+                            showRecordScoreList[sItem.sIndex].detail_list
+                          "
                         >
                           <span
                             class="record-score-content"
@@ -1288,7 +1292,11 @@
                           >评分</el-button
                         >
                         <template
-                          v-if="showRecordScoreList[sItem.sIndex] && showRecordScoreList[sItem.sIndex].detail_list"
+                          v-if="
+                            assessmentMode &&
+                            showRecordScoreList[sItem.sIndex] &&
+                            showRecordScoreList[sItem.sIndex].detail_list
+                          "
                         >
                           <span
                             class="record-score-content"
@@ -1428,12 +1436,14 @@
                   :standard-url-list="data.new_word_list"
                   :word-time="data.new_word_list"
                   :sent-list="sentList"
+                  :show-scoring="assessmentMode"
                   @changeWholeStatus="changeWholeStatus"
                   @getRecordIndex="getRecordScoreIndex"
                   @getWholeRecordIndex="getWholeRecordIndex"
                   @handleDeleteWholeRecord="handleDeleteWholeRecord"
                   @updataWholeRecordList="updataWholeRecordList"
                   @updataWholeScoreAndRecord="updataWholeScoreAndRecord"
+                  @changeIsRecord="changeIsRecord"
                 />
               </div>
             </div>
@@ -1880,6 +1890,7 @@ export default {
       stopAudios: false,
       assessmentMode: false, // 是否开启录音评分模式
       isShowScoreBtn: false, // 是否显示评分按钮
+      isRecording: false, // 整体录音是否在开启中
     };
   },
   computed: {
@@ -2032,6 +2043,10 @@ export default {
 
           // 有评分按钮
           this.rowWidth += 50;
+          if (this.isHasScore) {
+            // 有评分
+            this.rowWidth += 88;
+          }
           if (this.rowWidth < this.width - 2) {
             this.rowWidth = this.width - 2;
           }
@@ -2087,6 +2102,10 @@ export default {
           this.rowWidth += 180;
           // 有评分按钮
           this.rowWidth += 50;
+          if (this.isHasScore) {
+            // 有分数
+            this.rowWidth += 88;
+          }
           if (this.rowWidth < this.width - 2) {
             this.rowWidth = this.width - 2;
           }
@@ -2590,7 +2609,7 @@ export default {
     getWholeRecordIndex(index, result) {
       this.wholeRecordIndex = index;
       this.showRecordScoreList = result;
-      this.isHasScore = true;
+      // this.isHasScore = true;
       // let indexs = 0;
       // this.answer.answer_list.forEach((item) => {
       //   item.forEach((items) => {
@@ -2680,6 +2699,18 @@ export default {
     getPlayStatus(val) {
       // this.isPlaying = val;
     },
+    toggleAssessmentMode() {
+      if (this.isRecording) return;
+
+      this.$nextTick(() => {
+        this.assessmentMode = !this.assessmentMode;
+        console.log(this.assessmentMode);
+        this.$forceUpdate();
+      });
+    },
+    changeIsRecord(flag) {
+      this.isRecording = flag;
+    },
   },
 };
 </script>