From fe5c4215f794630576067cef223479b74deadd3e Mon Sep 17 00:00:00 2001 From: Blizzard Date: Wed, 22 Jul 2026 14:47:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(voice):=20=E9=BA=A6=E5=85=8B=E9=A3=8E?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F=E5=85=9C=E5=BA=95=E2=80=94=E2=80=94WKWebView?= =?UTF-8?q?=20=E4=B8=8D=E8=AE=A4=E9=AB=98=E7=BA=A7=E7=BA=A6=E6=9D=9F("Inva?= =?UTF-8?q?lid=20constraint")=E9=80=80=20{audio:true}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原生壳(Wails WKWebView)的 getUserMedia 不认 channelCount/echoCancellation 等高级约束, 抛 "Invalid constraint"。先试高级约束(浏览器更好),失败退回最简 {audio:true}(最兼容)。 配合上一提交的 .app 打包+NSMicrophoneUsageDescription,原生壳麦克风打通。 Co-Authored-By: Claude Opus 4.8 --- sundynix-desktop/frontend/src/lib/voice.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sundynix-desktop/frontend/src/lib/voice.ts b/sundynix-desktop/frontend/src/lib/voice.ts index cc4b4df..7508c18 100644 --- a/sundynix-desktop/frontend/src/lib/voice.ts +++ b/sundynix-desktop/frontend/src/lib/voice.ts @@ -201,9 +201,16 @@ export class VoiceClient { if (!navigator.mediaDevices?.getUserMedia) { throw new Error("此窗口暂不支持麦克风(原生壳需授权/安全上下文)。用浏览器打开 localhost:5173 可直接说话。"); } - const stream = await navigator.mediaDevices.getUserMedia({ - audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true }, - }); + // 先试带回声消除等约束(浏览器更好),WKWebView(原生壳)不认高级约束会抛 "Invalid constraint", + // 退回最简 {audio:true}(最兼容;多数实现默认已开回声消除)。 + let stream: MediaStream; + try { + stream = await navigator.mediaDevices.getUserMedia({ + audio: { echoCancellation: true, noiseSuppression: true, channelCount: 1 }, + }); + } catch { + stream = await navigator.mediaDevices.getUserMedia({ audio: true }); + } this.micStream = stream; const ctx = new AudioContext(); this.micCtx = ctx;