fix(voice): 浏览器语音默认走服务端 ASR

Web Speech 依赖 Google,国内浏览器常无法转文字;支持麦克风时改为录音后走自有 ASR,并补全网络异常时的降级匹配。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-06-29 12:34:34 +08:00
parent 0187f26c5b
commit 08c5b22d4a
2 changed files with 18 additions and 10 deletions
+14 -4
View File
@@ -2,16 +2,26 @@ import { isWechatBrowser } from '../utils/wechat';
import { MicCapture } from './micCapture';
import { isSpeechRecognitionSupported } from './speechRecognition';
/** 微信内置浏览器 Web Speech 不可用,直接走服务端 ASR。 */
/**
* 优先服务端 ASR(录音后识别):
* - 微信内 Web Speech 不可用;
* - 普通浏览器若支持麦克风,也走自有 ASR(Web Speech 依赖 Google,国内常无结果)。
*/
export function shouldPreferServerAsr(): boolean {
return isWechatBrowser();
if (isWechatBrowser()) return true;
return MicCapture.isSupported();
}
/** Web Speech 出现这些错误时可改用服务端 ASR(录音 + 完成识别)。 */
/** 仍尝试 Web Speech 时,出现这些错误可降级为服务端 ASR。 */
export function shouldFallbackToServerAsr(errorMessage: string): boolean {
if (shouldPreferServerAsr()) return false;
if (!MicCapture.isSupported()) return false;
return /network|service-not-allowed|语音识别失败|无法启动语音识别/i.test(errorMessage);
return /network|网络异常|service-not-allowed|语音识别失败|无法启动语音识别/i.test(errorMessage);
}
/** 是否使用浏览器 Web Speech 实时听写(仅在不走服务端 ASR 且 API 可用时)。 */
export function shouldUseLiveSpeech(): boolean {
return isSpeechRecognitionSupported() && !shouldPreferServerAsr();
}
/** Whether voice input may work in this browser (mic and/or live speech). */