[GH-ISSUE #3382] [Issue]: TTS clip is dropped (not played) when the tab is unfocused as generation finishes #1832

Closed
opened 2026-10-21 05:43:05 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jul 8, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3382

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

If you trigger TTS and switch away from the tab before the audio finishes generating, the clip is dropped instead of played. TTS is fetched asynchronously and Audio.play() is called only after the fetch resolves — so if the tab is hidden at that moment, the browser's autoplay policy rejects play() and the clip is discarded (an error state is set, but nothing is played or retried).

How to reproduce (try it yourself)

  1. Trigger TTS on a message (or let auto-TTS fire) with a voice that takes a moment to generate (e.g. ElevenLabs).
  2. Immediately switch to another browser tab so Marinara's tab is hidden while the audio is still generating.
  3. Switch back after it finishes.

You'll see: the clip never played (state went to an error / "Browser blocked audio playback"), and it isn't retried when you return.
Expected: the clip plays when you return to the tab — it should be queued and played on the next focus/visibility change instead of being dropped.

Where it is

packages/client/src/lib/tts-service.ts — the audio element is created after the async fetch: blob = await this.getAudioBlob(...) (192), then const audio = new Audio(objectUrl) (218), then await audio.play() (235). The catch (236-243) handles a rejected play() by calling this.cleanup() and setState("error") with "Browser blocked audio playback" — there's no queue or retry. A search for visibilitychange / document.hidden in this file finds nothing, so a hidden tab at play-time is unhandled.

What's happening

Browser autoplay policy blocks play() for audible media when the document is hidden and there's no fresh user gesture by the time the async fetch resolves. Because the play call happens post-fetch, a tab switch during generation lands exactly in that blocked window, and the code treats the rejection as a terminal error rather than a deferrable one.

Suggested fix

When audio.play() rejects (or document.hidden is true at play-time), don't discard the clip — hold it and retry playback on the next visibilitychange/focus, guarded so it only plays if it's still the current sequence. Revoke the object URL if the user has moved on. This keeps the existing "generate-then-play" flow but survives a backgrounded tab.


AI-investigated against the current tree; the post-fetch new Audio().play() path, the terminal-error catch, and the absence of any visibility handling were all read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jul 8, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3382 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** If you trigger TTS and switch away from the tab before the audio finishes generating, the clip is dropped instead of played. TTS is fetched asynchronously and `Audio.play()` is called only *after* the fetch resolves — so if the tab is hidden at that moment, the browser's autoplay policy rejects `play()` and the clip is discarded (an error state is set, but nothing is played or retried). **How to reproduce (try it yourself)** 1. Trigger TTS on a message (or let auto-TTS fire) with a voice that takes a moment to generate (e.g. ElevenLabs). 2. Immediately switch to another browser tab so Marinara's tab is hidden while the audio is still generating. 3. Switch back after it finishes. **You'll see:** the clip never played (state went to an error / "Browser blocked audio playback"), and it isn't retried when you return. **Expected:** the clip plays when you return to the tab — it should be queued and played on the next focus/visibility change instead of being dropped. **Where it is** `packages/client/src/lib/tts-service.ts` — the audio element is created *after* the async fetch: `blob = await this.getAudioBlob(...)` (192), then `const audio = new Audio(objectUrl)` (218), then `await audio.play()` (235). The catch (236-243) handles a rejected `play()` by calling `this.cleanup()` and `setState("error")` with "Browser blocked audio playback" — there's no queue or retry. A search for `visibilitychange` / `document.hidden` in this file finds nothing, so a hidden tab at play-time is unhandled. **What's happening** Browser autoplay policy blocks `play()` for audible media when the document is hidden and there's no fresh user gesture by the time the async fetch resolves. Because the play call happens post-fetch, a tab switch during generation lands exactly in that blocked window, and the code treats the rejection as a terminal error rather than a deferrable one. **Suggested fix** When `audio.play()` rejects (or `document.hidden` is true at play-time), don't discard the clip — hold it and retry playback on the next `visibilitychange`/focus, guarded so it only plays if it's still the current sequence. Revoke the object URL if the user has moved on. This keeps the existing "generate-then-play" flow but survives a backgrounded tab. --- *AI-investigated against the current tree; the post-fetch `new Audio().play()` path, the terminal-error catch, and the absence of any visibility handling were all read and confirmed. Please verify before fixing.*
bsakaguchi 2026-10-21 05:43:05 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Jul 8, 2026):

Addressed in #3388 and merged to staging.

<!-- gh-comment-id:4918159199 --> @SpicyMarinara commented on GitHub (Jul 8, 2026): Addressed in #3388 and merged to staging.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1832
No description provided.