From 90c07de469b86788f7069c5305a945bbfbb2760c Mon Sep 17 00:00:00 2001 From: Li-Yu Yu Date: Fri, 24 Jul 2026 04:13:12 +0000 Subject: [PATCH] Close AudioContext when recording visualization stops Each recording created a new AudioContext for the waveform AnalyserNode but never closed it. Browsers limit the number of concurrent AudioContexts, so after ~40 record/stop cycles new contexts fail to start: getByteFrequencyData() returns silence (the waveform stops appearing) and audio decode/playback breaks until the page is refreshed. Close the AudioContext in the draw loop once the stream is no longer active, mirroring the cleanup already done in the download handler. Bug: b/538354264 --- src/pwa-audio-recorder/app.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pwa-audio-recorder/app.mjs b/src/pwa-audio-recorder/app.mjs index 35f0106..a6a7e6a 100644 --- a/src/pwa-audio-recorder/app.mjs +++ b/src/pwa-audio-recorder/app.mjs @@ -234,7 +234,13 @@ function visualizeRecording({stream, outlineIndicator, waveformIndicator}) { /** Repeatedly draws the waveform and loudness indicator. */ function draw() { if (!stream.active) { - return; // Stop drawing loop once the recording stopped. + // Stop drawing loop once the recording stopped, and release the + // AudioContext. Browsers limit the number of concurrent AudioContexts, so + // failing to close it here leaks one context per recording and, after a + // few dozen recordings, prevents new contexts from starting (the waveform + // stops appearing and playback breaks until the page is refreshed). + audioCtx.close(); + return; } analyser.getByteFrequencyData(dataArray);