diff --git a/package.json b/package.json index d70304a..2d70d0a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-worker-station", - "version": "1.0.32", + "version": "1.0.33", "description": "WMS仓库工作台桌面端", "author": "tianbao", "main": "electron/main.js", diff --git a/src/core/voice.js b/src/core/voice.js index 68fd9ed..9c3c072 100644 --- a/src/core/voice.js +++ b/src/core/voice.js @@ -9,10 +9,8 @@ let instance = null class Voice { constructor() { - this.audio = uni.createInnerAudioContext() - this.audio.onError((e) => { - console.log('[voice] audio error:', e) - }) + // 每次播放都会重建音频实例,这里仅占位 + this.audio = null } /** 仅播放语音(不弹 toast,避免与全局 showToast 拦截器互相递归) */ @@ -20,8 +18,11 @@ class Voice { if (!msg) { return } + // 末字补句号(两个以加长停顿)+ 下载到本地再播:与打包页 speakViaHttp 行为一致, + // 避免部分 Android 设备播放远程 MP3 时解码丢尾帧导致末字截断 + const ttsText = String(msg) + '。。' uni.request({ - url: VOICE_API + msg, + url: VOICE_API + encodeURIComponent(ttsText), method: 'GET', timeout: 3000, header: { 'content-type': 'text/xml; charset=utf-8' }, @@ -30,15 +31,83 @@ class Voice { return } const src = VOICE_BASE + res.data.audio - this.audio.stop() - this.audio.src = src - this.audio.play() + this.downloadVoiceAudio(src) + .then((localSrc) => this.playVoiceAudio(localSrc)) + .catch(() => this.playVoiceAudio(src)) }, fail: (err) => { console.log('[voice] request fail:', err) }, }) } + + /** 下载音频到本地临时文件;失败拒绝,由调用方回退远程地址 */ + downloadVoiceAudio(src) { + return new Promise((resolve, reject) => { + uni.downloadFile({ + url: src, + timeout: 5000, + success: (res) => { + if (res.statusCode === 200 && res.tempFilePath) { + console.log('[voice] 音频已下载到本地:', res.tempFilePath) + resolve(res.tempFilePath) + } else { + reject(new Error('下载状态异常: ' + res.statusCode)) + } + }, + fail: (err) => { + reject(err) + }, + }) + }) + } + + /** 每次新建音频实例:等缓冲就绪再播放,500ms 兜底 */ + playVoiceAudio(src) { + try { + if (this.audio) { + try { + this.audio.stop() + this.audio.destroy() + } catch (e) { + // 忽略旧实例释放异常 + } + this.audio = null + } + const audio = uni.createInnerAudioContext() + this.audio = audio + let playStarted = false + let finished = false + const doPlay = () => { + if (playStarted || finished) { + return + } + playStarted = true + try { + audio.play() + console.log('[voice] 音频已开始播放') + } catch (e) { + console.log('[voice] audio play error:', e) + } + } + audio.onError((e) => { + finished = true + console.log('[voice] audio error:', e) + }) + audio.onEnded(() => { + finished = true + console.log('[voice] 播放结束,总时长:', audio.duration, 's,实际播到:', audio.currentTime, 's') + }) + audio.onCanplay(() => { + console.log('[voice] 音频可播放,总时长:', audio.duration, 's,来源:', String(src).startsWith('http') ? '远程' : '本地') + doPlay() + }) + audio.src = src + setTimeout(doPlay, 500) + } catch (e) { + console.log('[voice] 创建音频上下文异常:', e) + } + } } /** 获取语音单例(首次调用时初始化,确保 uni 运行时就绪) */ diff --git a/src/pages/packaging/index.vue b/src/pages/packaging/index.vue index 3467f98..d4f7077 100644 --- a/src/pages/packaging/index.vue +++ b/src/pages/packaging/index.vue @@ -1395,9 +1395,9 @@ export default { resolve(ok) } } - // 末字补一个句号:让 TTS 在最后一个字后生成停顿,避免部分 Android 设备 + // 末字补句号(两个以加长停顿):让 TTS 在最后一个字后生成停顿,避免部分 Android 设备 // 播放 HTTP 音频时解码丢尾帧,导致最后一个字(如“包”)被截断 - const ttsText = String(msg) + '。' + const ttsText = String(msg) + '。。' uni.request({ url: VOICE_API_URL + encodeURIComponent(ttsText), method: 'GET', @@ -1413,9 +1413,18 @@ export default { } const src = VOICE_API_PATH + audioPath console.log('[语音播报] HTTP 音频地址:', src) - this.playVoiceAudio(src, finish) - // 超时兜底:5 秒后仍未播放完成也视为完成,避免一直等待 - timeoutTimer = setTimeout(() => finish(true), 5000) + // 先整体下载到本地临时文件再播放:避免内网/流式播放缓冲不足时尾帧被截断 + this.downloadVoiceAudio(src) + .then((localSrc) => { + this.playVoiceAudio(localSrc, finish) + // 超时兜底:5 秒后仍未播放完成也视为完成,避免一直等待 + timeoutTimer = setTimeout(() => finish(true), 5000) + }) + .catch(() => { + // 下载失败回退为直接播远程地址 + this.playVoiceAudio(src, finish) + timeoutTimer = setTimeout(() => finish(true), 5000) + }) } catch (e) { console.error('[语音播报] HTTP 接口解析异常:', e) finish(false) @@ -1428,6 +1437,25 @@ export default { }) }) }, + downloadVoiceAudio(src) { + return new Promise((resolve, reject) => { + uni.downloadFile({ + url: src, + timeout: 5000, + success: (res) => { + if (res.statusCode === 200 && res.tempFilePath) { + console.log('[语音播报] 音频已下载到本地:', res.tempFilePath) + resolve(res.tempFilePath) + } else { + reject(new Error('下载状态异常: ' + res.statusCode)) + } + }, + fail: (err) => { + reject(err) + }, + }) + }) + }, playVoiceAudio(src, onEnd) { try { if (this.voiceAudioContext) { @@ -1462,11 +1490,15 @@ export default { }) audio.onEnded(() => { finished = true + console.log('[语音播报] 播放结束,总时长:', audio.duration, 's,实际播到:', audio.currentTime, 's') onEnd(true) }) // 等缓冲就绪再播放:部分 Android 设备设置 src 后立即 play 会在缓冲不足时 // 截断尾帧(末字丢失);onCanplay 后再播更稳,500ms 兜底防止事件不触发 - audio.onCanplay(doPlay) + audio.onCanplay(() => { + console.log('[语音播报] 音频可播放,总时长:', audio.duration, 's,来源:', String(src).startsWith('http') ? '远程' : '本地') + doPlay() + }) audio.src = src setTimeout(doPlay, 500) } catch (e) { @@ -3621,8 +3653,9 @@ export default { display: flex; flex-wrap: wrap; align-items: center; - gap: 0.307rem 1.229rem; margin-top: 0.307rem; + /* 兼容不支持 flex gap 的旧版 WebView / 小程序:用负外边距抵消末行/末项多余间距 */ + margin-bottom: -0.307rem; } .pack-meta-item { @@ -3630,6 +3663,12 @@ export default { flex-direction: row; align-items: center; min-width: 0; + margin-right: 1.229rem; + margin-bottom: 0.307rem; +} + +.pack-meta-item:last-child { + margin-right: 0; } .pack-meta-label {