fix(voice): 麦克风约束兜底——WKWebView 不认高级约束("Invalid constraint")退 {audio:true}
原生壳(Wails WKWebView)的 getUserMedia 不认 channelCount/echoCancellation 等高级约束,
抛 "Invalid constraint"。先试高级约束(浏览器更好),失败退回最简 {audio:true}(最兼容)。
配合上一提交的 .app 打包+NSMicrophoneUsageDescription,原生壳麦克风打通。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -201,9 +201,16 @@ export class VoiceClient {
|
|||||||
if (!navigator.mediaDevices?.getUserMedia) {
|
if (!navigator.mediaDevices?.getUserMedia) {
|
||||||
throw new Error("此窗口暂不支持麦克风(原生壳需授权/安全上下文)。用浏览器打开 localhost:5173 可直接说话。");
|
throw new Error("此窗口暂不支持麦克风(原生壳需授权/安全上下文)。用浏览器打开 localhost:5173 可直接说话。");
|
||||||
}
|
}
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({
|
// 先试带回声消除等约束(浏览器更好),WKWebView(原生壳)不认高级约束会抛 "Invalid constraint",
|
||||||
audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true },
|
// 退回最简 {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;
|
this.micStream = stream;
|
||||||
const ctx = new AudioContext();
|
const ctx = new AudioContext();
|
||||||
this.micCtx = ctx;
|
this.micCtx = ctx;
|
||||||
|
|||||||
Reference in New Issue
Block a user