fix: recorderStart 异步竞争条件

微信 RecorderManager.start() 是异步的,onStart 回调触发前
调用 stop() 会报 'operateRecorder:fail recorder not start'。

- 增加 recorderStarted 标志,onStart 时置 true
- 增加 pendingStop 标志,stop() 在 start 前被调用时延迟执行
- onError 时全面清理 pendingStop/recorderStarted/skipAsr 状态
This commit is contained in:
yuzhiran
2026-07-05 18:06:17 +08:00
parent 48354e634b
commit 462884a321
@@ -157,6 +157,8 @@ const asrLoading = ref(false)
let recorder = null
let recordTimer = null
let skipAsr = false
let recorderStarted = false
let pendingStop = false
function initRecorder() {
// #ifdef MP-WEIXIN
@@ -164,12 +166,20 @@ function initRecorder() {
recorder = uni.getRecorderManager()
recorder.onStart(() => {
console.log('[Recorder] start recording')
recorderStarted = true
recordingDuration.value = 0
recordTimer = setInterval(() => { recordingDuration.value++ }, 1000)
if (pendingStop) {
pendingStop = false
recorder.stop()
}
})
recorder.onError((err) => {
console.error('[Recorder] error:', JSON.stringify(err))
isRecording.value = false
recorderStarted = false
pendingStop = false
skipAsr = false
recordingDuration.value = 0
asrLoading.value = false
if (recordTimer) { clearInterval(recordTimer); recordTimer = null }
@@ -179,6 +189,7 @@ function initRecorder() {
})
recorder.onStop(async (res) => {
isRecording.value = false
recorderStarted = false
recordingDuration.value = 0
if (recordTimer) { clearInterval(recordTimer); recordTimer = null }
if (skipAsr) { skipAsr = false; return }
@@ -459,6 +470,10 @@ function stopRecord() {
if (recordTimer) { clearInterval(recordTimer); recordTimer = null }
recordingDuration.value = 0
}
if (!recorderStarted) {
pendingStop = true
return
}
recorder.stop()
}
</script>