From 95ac9029c648194c35dda58daaa17b9160bcb69d Mon Sep 17 00:00:00 2001 From: 董国庆 <364620639@qq.com> Date: 星期三, 21 五月 2025 11:37:27 +0800 Subject: [PATCH] 2.0冒烟版本 --- H5/components/voiceInputPopup.vue | 104 ++++++++++++++++++++++++++------------------------- 1 files changed, 53 insertions(+), 51 deletions(-) diff --git a/H5/components/voiceInputPopup.vue b/H5/components/voiceInputPopup.vue index b0393ee..790f483 100644 --- a/H5/components/voiceInputPopup.vue +++ b/H5/components/voiceInputPopup.vue @@ -107,7 +107,7 @@ console.log('当前录音片段URL:', res.localUrl) console.log('当前已存储的片段数:', this.recordSegments.length) console.log('当前片段:', this.currentSegment) - + // 只有当当前片段与上一个不同时才添加 if (!this.currentSegment || this.currentSegment.url !== res.localUrl) { console.log('添加新的录音片段') @@ -183,7 +183,7 @@ try { const audioContext = new (window.AudioContext || window.webkitAudioContext)(); const audioBuffers = []; - + // 加载所有音频文件 for (const url of audioUrls) { const response = await fetch(url); @@ -194,7 +194,7 @@ // 计算总时长 const totalLength = audioBuffers.reduce((acc, buffer) => acc + buffer.length, 0); - + // 创建新的音频缓冲区 const mergedBuffer = audioContext.createBuffer( audioBuffers[0].numberOfChannels, @@ -215,7 +215,7 @@ // 将合并后的音频转换为 Blob const wavBlob = await this.audioBufferToWav(mergedBuffer); const mergedUrl = URL.createObjectURL(wavBlob); - + return mergedUrl; } catch (error) { console.error('合并音频失败:', error); @@ -284,65 +284,67 @@ if (this.isRecording) { this.$refs.mumuRecorder.stop() } - + // 停止计时 this.stopTimer() - + // 重置状态 this.isRecording = false this.isPaused = false this.currentSegment = null - - // 处理录音片段 - if (this.recordSegments.length > 0) { - try { - uni.showLoading({ - title: '正在处理音频...' - }) - - // 获取所有录音片段的URL - const audioUrls = this.recordSegments.map(segment => segment.url) - console.log('准备合并的音频片段列表:', audioUrls) - console.log('音频片段数量:', audioUrls.length) - - // 合并音频文件 - const mergedUrl = await this.mergeAudioFiles(audioUrls) - - // 创建合并后的音频数据对象 - const mergedAudioData = { - url: this.recordSegments[this.recordSegments.length - 1].url, - data: this.recordSegments[this.recordSegments.length - 1].data, - duration: this.seconds + this.minutes * 60 + this.hours * 3600 + setTimeout(async() => { + console.log('处理录音片段') + // 处理录音片段 + if (this.recordSegments.length > 0) { + try { + uni.showLoading({ + title: '正在处理音频...' + }) + + // 获取所有录音片段的URL + const audioUrls = this.recordSegments.map(segment => segment.url) + console.log('准备合并的音频片段列表:', audioUrls) + console.log('音频片段数量:', audioUrls.length) + + // 合并音频文件 + const mergedUrl = await this.mergeAudioFiles(audioUrls) + + // 创建合并后的音频数据对象 + const mergedAudioData = { + url: this.recordSegments[this.recordSegments.length - 1].url, + data: this.recordSegments[this.recordSegments.length - 1].data, + duration: this.seconds + this.minutes * 60 + this.hours * 3600 + } + + uni.showToast({ + title: '录制成功', + icon: 'success', + duration: 2000 + }) + + setTimeout(() => { + this.$emit('submit', mergedAudioData) + }, 2000) + + uni.hideLoading() + this.closePopup() + } catch (error) { + console.error('音频合并失败:', error) + uni.hideLoading() + uni.showToast({ + title: '音频合并失败,请重试', + icon: 'none', + duration: 2000 + }) } - + } else { uni.showToast({ - title: '录制成功', - icon: 'success', - duration: 2000 - }) - - setTimeout(() => { - this.$emit('submit', mergedAudioData) - }, 2000) - - uni.hideLoading() - this.closePopup() - } catch (error) { - console.error('音频合并失败:', error) - uni.hideLoading() - uni.showToast({ - title: '音频合并失败,请重试', + title: '未检测到录音文件', icon: 'none', duration: 2000 }) } - } else { - uni.showToast({ - title: '未检测到录音文件', - icon: 'none', - duration: 2000 - }) - } + }, 1000); } }, beforeDestroy() { -- Gitblit v1.7.1