fix: 修复安卓 Chrome 无音频问题,移除强制 16kHz 采样率,添加 AudioContext resume
This commit is contained in:
13
web/app.js
13
web/app.js
@@ -173,9 +173,12 @@
|
|||||||
// ── Audio pipeline ──
|
// ── Audio pipeline ──
|
||||||
async function initAudio() {
|
async function initAudio() {
|
||||||
if (state.audioCtx) return;
|
if (state.audioCtx) return;
|
||||||
const audioCtx = new (window.AudioContext || window.webkitAudioContext)({
|
// Use device native sample rate — we resample to 16kHz in software
|
||||||
sampleRate: TARGET_SAMPLE_RATE, // Request 16kHz directly (Chrome supports)
|
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");
|
await audioCtx.audioWorklet.addModule("audio-processor.js");
|
||||||
state.audioCtx = audioCtx;
|
state.audioCtx = audioCtx;
|
||||||
}
|
}
|
||||||
@@ -183,6 +186,10 @@
|
|||||||
if (state.recording) return;
|
if (state.recording) return;
|
||||||
try {
|
try {
|
||||||
await initAudio();
|
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({
|
const stream = await navigator.mediaDevices.getUserMedia({
|
||||||
audio: { echoCancellation: true, noiseSuppression: true, channelCount: 1 },
|
audio: { echoCancellation: true, noiseSuppression: true, channelCount: 1 },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user