|
|
@@ -251,7 +251,10 @@ export default {
|
|
|
},
|
|
|
watch: {
|
|
|
answerRecordList(newVal) {
|
|
|
- this.recordList = JSON.parse(JSON.stringify(newVal));
|
|
|
+ // 录音进行中不接收父组件回灌:父组件保存的是上一次回传的快照,
|
|
|
+ // 用它覆盖会把刚开始录、还没回传的那一条冲掉,之后录音就只能写回到旧条目上
|
|
|
+ if (this.microphoneStatus) return;
|
|
|
+ this.recordList = Array.isArray(newVal) ? JSON.parse(JSON.stringify(newVal)) : [];
|
|
|
this.oldIndex = null;
|
|
|
this.selectIndex = this.recordList.length > 0 ? this.recordList.length - 1 : null;
|
|
|
if (this.recordList.length > 0 && this.type === 'normal') {
|
|
|
@@ -321,117 +324,203 @@ export default {
|
|
|
if (this.microphoneStatus) {
|
|
|
this.handleStopRecording();
|
|
|
} else {
|
|
|
- this.hasMicro = '';
|
|
|
- this.$root.isRecording = true;
|
|
|
- this.$emit('getWavblob', null);
|
|
|
- this.$emit('getSelectData', { type: '' });
|
|
|
- // 开始录音
|
|
|
- this.getRecorder().start();
|
|
|
- this.microphoneStatus = true;
|
|
|
- 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.round(Math.random() * 10)).toString(),
|
|
|
- };
|
|
|
- if (this.selectData) obj.selectData = this.selectData;
|
|
|
- this.recordList.push(obj);
|
|
|
- this.recordFile += 1;
|
|
|
- this.selectIndex = this.recordList.length - 1;
|
|
|
+ this.startRecording();
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
/**
|
|
|
+ * 开始录音。
|
|
|
+ * start() 内部要先走 getUserMedia,成功后才真正建立音频链路;
|
|
|
+ * 这段时间里用户可能马上再点一次停止,若此时就销毁录音器,
|
|
|
+ * getUserMedia 的回调会拿不到可用的 AudioContext(麦克风流不会被释放,下一次 start() 也可能失败),
|
|
|
+ * 表现为“有时录不了下一条”。所以把启动过程记录下来,停止时先等它结束。
|
|
|
+ */
|
|
|
+ startRecording() {
|
|
|
+ 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.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.round(Math.random() * 10)).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;
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 等待 getUserMedia 完成。
|
|
|
+ * 未就绪就调用 stop()/destroy() 会把正在建立的音频链路拆掉,导致本次录音无效、麦克风不释放。
|
|
|
+ * @returns {Promise<boolean>} 是否启动成功
|
|
|
+ */
|
|
|
+ async waitRecordStart() {
|
|
|
+ const startPromise = this.recordStartPromise;
|
|
|
+ if (!startPromise) return true;
|
|
|
+ this.recordStartPromise = null;
|
|
|
+ try {
|
|
|
+ await startPromise;
|
|
|
+ return true;
|
|
|
+ } catch (err) {
|
|
|
+ this.handleRecordStartFail(err);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 录音启动失败(多为未授权麦克风):清掉占位录音并复位状态,否则按钮会一直停在“录音中”,后面再也录不了
|
|
|
+ handleRecordStartFail(err) {
|
|
|
+ console.warn('[audio] 录音启动失败:', (err && (err.message || err.name)) || err);
|
|
|
+ clearInterval(this.timer);
|
|
|
+ this.$root.isRecording = false;
|
|
|
+ this.microphoneStatus = false;
|
|
|
+ this.recordStartPromise = null;
|
|
|
+ this.recordtime = 0;
|
|
|
+ this.hasMicro = '';
|
|
|
+ // 启动失败的录音器可能停在不可用状态,丢掉缓存,下次重新创建
|
|
|
+ if (this.recorderInstance) releaseRecorder(this.recorderInstance);
|
|
|
+ this.recorderInstance = null;
|
|
|
+ this.removeRecording(this.recordingId);
|
|
|
+ this.$message.warning('录音启动失败,请检查麦克风权限后重试');
|
|
|
+ },
|
|
|
+ // 丢弃一条没有保存成功的录音(启动失败、保存异常时使用)
|
|
|
+ removeRecording(id) {
|
|
|
+ const index = id ? this.recordList.findIndex((record) => record && record.id === id) : -1;
|
|
|
+ if (this.recordingId === id) this.recordingId = null;
|
|
|
+ if (index < 0) return;
|
|
|
+ this.recordList.splice(index, 1);
|
|
|
+ this.selectIndex = this.recordList.length > 0 ? this.recordList.length - 1 : null;
|
|
|
+ this.hasMicro = this.recordList.length > 0 ? 'normal' : '';
|
|
|
+ this.recordtime = this.recordList.length > 0 ? this.recordList[this.selectIndex].toltime || 0 : 0;
|
|
|
+ this.$emit('handleWav', JSON.parse(JSON.stringify(this.recordList)), this.tmIndex, this.index, this.indexs);
|
|
|
+ },
|
|
|
+ /**
|
|
|
* 停止录音并把录音上传到文件服务器。
|
|
|
* 录音先以 dataURL 写回答案(结构完整、可立即试听),上传成功后再替换为 file_id,
|
|
|
* 避免答案里内联 MB 级 base64 导致提交请求超时。
|
|
|
*/
|
|
|
- handleStopRecording() {
|
|
|
+ async handleStopRecording() {
|
|
|
+ // 连点时防止重复走停止流程(会重复上传、重复写回答案)
|
|
|
+ if (this.stopping) return;
|
|
|
+ this.stopping = true;
|
|
|
const recorder = this.getRecorder();
|
|
|
- recorder.stop();
|
|
|
- this.$root.isRecording = false;
|
|
|
- clearInterval(this.timer);
|
|
|
-
|
|
|
- const toltime = Math.floor(recorder.duration); // 录音总时长
|
|
|
- const fileSize = recorder.fileSize; // 录音总大小
|
|
|
- const wav = recorder.getWAVBlob(); // 获取 WAV 数据
|
|
|
- // 录音结束立即释放录音占用的 AudioContext 与麦克风流,避免多次录音不断累积
|
|
|
- releaseRecorder(recorder);
|
|
|
- this.microphoneStatus = false;
|
|
|
- this.hasMicro = 'normal';
|
|
|
-
|
|
|
- const index = this.selectIndex;
|
|
|
- const target = this.recordList[index];
|
|
|
- if (!target) {
|
|
|
- this.hasMicro = '';
|
|
|
- return;
|
|
|
- }
|
|
|
- const targetId = target.id;
|
|
|
-
|
|
|
- /**
|
|
|
- * 重新定位当前列表里的同一条录音。
|
|
|
- * handleWav 会把录音列表回传给父组件,父组件再通过 answer-record-list 回灌,
|
|
|
- * watcher 会用深拷贝替换 recordList,之前持有的对象引用随即失效,
|
|
|
- * 因此每次写回都必须按 id 重新定位,否则上传成功后写入的 file_id 会丢失。
|
|
|
- */
|
|
|
- const findTarget = () => {
|
|
|
- const current = this.recordList[index];
|
|
|
- if (current && current.id === targetId) return current;
|
|
|
- return this.recordList.find((record) => record && record.id === targetId) || null;
|
|
|
- };
|
|
|
- const patchTarget = (patch) => {
|
|
|
- const current = findTarget();
|
|
|
- if (!current) return null;
|
|
|
- Object.keys(patch).forEach((key) => this.$set(current, key, patch[key]));
|
|
|
- return current;
|
|
|
- };
|
|
|
- const emitAnswer = () => {
|
|
|
- this.$emit('handleWav', JSON.parse(JSON.stringify(this.recordList)), this.tmIndex, this.index, this.indexs);
|
|
|
- const current = findTarget();
|
|
|
- if (current && current.selectData) this.$emit('getSelectData', current.selectData);
|
|
|
- };
|
|
|
+ try {
|
|
|
+ const started = await this.waitRecordStart();
|
|
|
+ if (!started || !this.microphoneStatus) return;
|
|
|
+
|
|
|
+ recorder.stop();
|
|
|
+ this.$root.isRecording = false;
|
|
|
+ clearInterval(this.timer);
|
|
|
+
|
|
|
+ const toltime = Math.floor(recorder.duration); // 录音总时长
|
|
|
+ const fileSize = recorder.fileSize; // 录音总大小
|
|
|
+ const wav = recorder.getWAVBlob(); // 获取 WAV 数据
|
|
|
+ // 录音结束立即释放录音占用的 AudioContext 与麦克风流,避免多次录音不断累积
|
|
|
+ releaseRecorder(recorder);
|
|
|
+ this.microphoneStatus = false;
|
|
|
+ this.hasMicro = 'normal';
|
|
|
|
|
|
- patchTarget({ toltime, fileSize, upload_state: 'uploading' });
|
|
|
-
|
|
|
- const task = blobToDataUrl(wav)
|
|
|
- // 先落地本地 dataURL:答案完整、可立即试听,同时作为上传失败时的兜底格式
|
|
|
- .then((dataUrl) => {
|
|
|
- patchTarget({ wavData: dataUrl });
|
|
|
- if (this.selectIndex === index) this.wavblob = dataUrl;
|
|
|
- this.$emit('getWavblob', dataUrl);
|
|
|
- emitAnswer();
|
|
|
- return uploadRecording(dataUrl);
|
|
|
- })
|
|
|
- // 上传成功:替换为 file_id,答案体积从 MB 级降到 KB 级
|
|
|
- .then((res) => {
|
|
|
- patchTarget({ wavData: res.file_id, file_url: res.file_url || '', upload_state: 'done' });
|
|
|
- if (this.selectIndex === index) this.wavblob = res.file_id;
|
|
|
- emitAnswer();
|
|
|
- })
|
|
|
- // 上传失败:保留 dataURL 并标记状态,由提交前统一决策
|
|
|
- .catch((err) => {
|
|
|
- const current = patchTarget({
|
|
|
- upload_state: 'failed',
|
|
|
- upload_error: (err && err.message) || '录音上传失败',
|
|
|
+ const index = this.selectIndex;
|
|
|
+ // 父组件回灌列表后索引可能变化,优先按 id 找本次录音的那一条
|
|
|
+ const target =
|
|
|
+ this.recordList.find((record) => record && record.id === this.recordingId) || this.recordList[index];
|
|
|
+ this.recordingId = null;
|
|
|
+ if (!target) {
|
|
|
+ this.hasMicro = '';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const targetId = target.id;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重新定位当前列表里的同一条录音。
|
|
|
+ * handleWav 会把录音列表回传给父组件,父组件再通过 answer-record-list 回灌,
|
|
|
+ * watcher 会用深拷贝替换 recordList,之前持有的对象引用随即失效,
|
|
|
+ * 因此每次写回都必须按 id 重新定位,否则上传成功后写入的 file_id 会丢失。
|
|
|
+ */
|
|
|
+ const findTarget = () => {
|
|
|
+ const current = this.recordList[index];
|
|
|
+ if (current && current.id === targetId) return current;
|
|
|
+ return this.recordList.find((record) => record && record.id === targetId) || null;
|
|
|
+ };
|
|
|
+ const patchTarget = (patch) => {
|
|
|
+ const current = findTarget();
|
|
|
+ if (!current) return null;
|
|
|
+ Object.keys(patch).forEach((key) => this.$set(current, key, patch[key]));
|
|
|
+ return current;
|
|
|
+ };
|
|
|
+ const emitAnswer = () => {
|
|
|
+ this.$emit('handleWav', JSON.parse(JSON.stringify(this.recordList)), this.tmIndex, this.index, this.indexs);
|
|
|
+ const current = findTarget();
|
|
|
+ if (current && current.selectData) this.$emit('getSelectData', current.selectData);
|
|
|
+ };
|
|
|
+
|
|
|
+ patchTarget({ toltime, fileSize, upload_state: 'uploading' });
|
|
|
+
|
|
|
+ const task = blobToDataUrl(wav)
|
|
|
+ // 先落地本地 dataURL:答案完整、可立即试听,同时作为上传失败时的兜底格式
|
|
|
+ .then((dataUrl) => {
|
|
|
+ patchTarget({ wavData: dataUrl });
|
|
|
+ if (this.selectIndex === index) this.wavblob = dataUrl;
|
|
|
+ this.$emit('getWavblob', dataUrl);
|
|
|
+ emitAnswer();
|
|
|
+ return uploadRecording(dataUrl);
|
|
|
+ })
|
|
|
+ // 上传成功:替换为 file_id,答案体积从 MB 级降到 KB 级
|
|
|
+ .then((res) => {
|
|
|
+ patchTarget({ wavData: res.file_id, file_url: res.file_url || '', upload_state: 'done' });
|
|
|
+ if (this.selectIndex === index) this.wavblob = res.file_id;
|
|
|
+ emitAnswer();
|
|
|
+ })
|
|
|
+ // 上传失败:保留 dataURL 并标记状态,由提交前统一决策
|
|
|
+ .catch((err) => {
|
|
|
+ const current = patchTarget({
|
|
|
+ upload_state: 'failed',
|
|
|
+ upload_error: (err && err.message) || '录音上传失败',
|
|
|
+ });
|
|
|
+ // 连本地 dataURL 都没拿到(FileReader 异常等),录音无法保存,提示重录
|
|
|
+ if (current && !current.wavData) this.$message.error('录音保存失败,请重新录制');
|
|
|
+ emitAnswer();
|
|
|
});
|
|
|
- // 连本地 dataURL 都没拿到(FileReader 异常等),录音无法保存,提示重录
|
|
|
- if (current && !current.wavData) this.$message.error('录音保存失败,请重新录制');
|
|
|
- emitAnswer();
|
|
|
- });
|
|
|
|
|
|
- // 同步登记,保证提交前的 drainUploads 能等到本次上传
|
|
|
- trackUpload(task);
|
|
|
+ // 同步登记,保证提交前的 drainUploads 能等到本次上传
|
|
|
+ trackUpload(task);
|
|
|
+ } catch (err) {
|
|
|
+ // 取录音数据失败时丢掉这一条,避免留下无法播放的录音
|
|
|
+ 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() {
|