fix(packaging): 语音播报末字截断第二轮修复 + 全局 toast 语音链路同步

- 文本末尾补「。。」加长停顿;先用 downloadFile 整段下载音频到本地再播,
  下载失败回退远程 src,缓解 Android 解码丢尾帧导致的末字截断
- src/core/voice.js 全局 toast 播报对齐打包页:补「。。」+ encodeURIComponent
  + 本地下载播放 + onCanplay 后 play + 500ms 兜底
- packaging 页 .pack-meta 移除 flex gap,改用 margin 兼容旧版 WebView
- 版本号 1.0.32 -> 1.0.33
This commit is contained in:
zm2000 2026-09-24 20:11:27 +08:00
parent 47130fbf53
commit 87b798659d
3 changed files with 124 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "wms-worker-station",
"version": "1.0.32",
"version": "1.0.33",
"description": "WMS仓库工作台桌面端",
"author": "tianbao",
"main": "electron/main.js",

View File

@ -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 运行时就绪) */

View File

@ -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 {