fix: interview 录音改为长按按住录音松开提交

startRecord 先同步设 isRecording=true 再异步 uni.authorize,避免
touchend 在 authorize success 回调前就触发 stopRecord 导致录音没
来得及开始就被停止。
移除冗余 doStartRecorder 函数,inline 到 authorize success 回调中。
stopRecord 不再检查 isRecording 守卫(可能已被 startRecord 设为 true),
直接 recorder.stop()。
This commit is contained in:
yuzhiran
2026-07-05 09:32:36 +08:00
parent fe688096a4
commit 656dfabb29
+20 -22
View File
@@ -401,11 +401,27 @@ function startRecord() {
if (aiLoading.value || isComplete.value) return if (aiLoading.value || isComplete.value) return
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
if (isRecording.value) return if (isRecording.value) return
// 先确保已获得 scope.record 授权,避免没权限时产生全 0 的 corrupted 音频 // 先标记正在录音,避免 touchend 在 authorize 回调前就触发 stopRecord
isRecording.value = true
uni.authorize({ uni.authorize({
scope: 'scope.record', scope: 'scope.record',
success: () => doStartRecorder(), success: () => {
if (!recorder) initRecorder()
if (!recorder) {
isRecording.value = false
uni.showToast({ title: '录音初始化失败', icon: 'none' })
return
}
recorder.start({
format: 'wav',
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 128000,
})
uni.vibrateShort({ type: 'medium' })
},
fail: () => { fail: () => {
isRecording.value = false
uni.showModal({ uni.showModal({
title: '需要录音权限', title: '需要录音权限',
content: '请在「设置」中开启「麦克风」权限后再使用语音输入', content: '请在「设置」中开启「麦克风」权限后再使用语音输入',
@@ -416,32 +432,14 @@ function startRecord() {
}) })
// #endif // #endif
// #ifndef MP-WEIXIN // #ifndef MP-WEIXIN
isRecording.value = false
uni.showToast({ title: '语音输入仅支持小程序', icon: 'none' }) uni.showToast({ title: '语音输入仅支持小程序', icon: 'none' })
// #endif // #endif
} }
// #ifdef MP-WEIXIN
function doStartRecorder() {
isRecording.value = true
if (!recorder) initRecorder()
if (!recorder) {
isRecording.value = false
uni.showToast({ title: '录音初始化失败', icon: 'none' })
return
}
recorder.start({
format: 'wav',
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 128000,
})
uni.vibrateShort({ type: 'medium' })
}
// #endif
function stopRecord() { function stopRecord() {
if (!recorder || !isRecording.value) return
isRecording.value = false isRecording.value = false
if (!recorder) return
recorder.stop() recorder.stop()
} }
</script> </script>