fix: 修复安卓 Chrome 无音频问题,移除强制 16kHz 采样率,添加 AudioContext resume

This commit is contained in:
2026-03-01 04:35:06 +08:00
parent 193f208d80
commit 5d17e2e9ac

View File

@@ -173,9 +173,12 @@
// ── Audio pipeline ──
async function initAudio() {
if (state.audioCtx) return;
const audioCtx = new (window.AudioContext || window.webkitAudioContext)({
sampleRate: TARGET_SAMPLE_RATE, // Request 16kHz directly (Chrome supports)
});
// Use device native sample rate — we resample to 16kHz in software
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Chrome requires resume() after user gesture
if (audioCtx.state === "suspended") {
await audioCtx.resume();
}
await audioCtx.audioWorklet.addModule("audio-processor.js");
state.audioCtx = audioCtx;
}
@@ -183,6 +186,10 @@
if (state.recording) return;
try {
await initAudio();
// Ensure AudioContext is running (may suspend between recordings)
if (state.audioCtx.state === "suspended") {
await state.audioCtx.resume();
}
const stream = await navigator.mediaDevices.getUserMedia({
audio: { echoCancellation: true, noiseSuppression: true, channelCount: 1 },
});