Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(editor): let a zero-length audio track read as ended

EtienneLescot6e5135f23e64
diff --git a/src/components/ai-edition/VirtualPreview.audio.test.ts b/src/components/ai-edition/VirtualPreview.audio.test.ts
index d1425bd79..31158ebd4 100644
--- a/src/components/ai-edition/VirtualPreview.audio.test.ts
+++ b/src/components/ai-edition/VirtualPreview.audio.test.ts
@@ -12,11 +12,20 @@ describe("resolveAudioTrackPlayback", () => {
expect(resolveAudioTrackPlayback(12, 10)).toEqual({ targetTimeSec: 10, shouldPlay: false });
});
+ it("treats a zero-length track as already ended", () => {
+ // An empty extraction is a KNOWN length, not an unknown one. Reading it as
+ // unknown parks the element at the video's time with shouldPlay true, and the
+ // rAF loop then seeks and calls play() on it for the whole timeline.
+ expect(resolveAudioTrackPlayback(1, 0)).toEqual({ targetTimeSec: 0, shouldPlay: false });
+ });
+
it("plays while the duration is still unknown", () => {
expect(resolveAudioTrackPlayback(1, Number.NaN)).toEqual({
targetTimeSec: 1,
shouldPlay: true,
});
+ // A negative duration is not a length either — same fallback as NaN.
+ expect(resolveAudioTrackPlayback(1, -1)).toEqual({ targetTimeSec: 1, shouldPlay: true });
});
it("never seeks to a negative time", () => {
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 44b47fb80..3f3dd524d 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -47,12 +47,17 @@ export interface VideoSource {
* only per-track difference that survives is length: the supplemental track is extracted
* separately and can end before the video does. Past its end the element is parked at its
* duration and paused rather than left seeking into nothing.
+ *
+ * Only an unusable duration falls back to Infinity — `NaN` before the element has its
+ * metadata, or a negative value. Zero is a real length, and the shortest track that is
+ * already over: an empty extraction has to read as ended, or the rAF loop below spends the
+ * whole timeline seeking and calling `play()` on an element that has nothing to play.
*/
export function resolveAudioTrackPlayback(
videoTimeSec: number,
durationSec = Number.POSITIVE_INFINITY,
) {
- const finiteDuration = Number.isFinite(durationSec) && durationSec > 0 ? durationSec : Infinity;
+ const finiteDuration = Number.isFinite(durationSec) && durationSec >= 0 ? durationSec : Infinity;
return {
targetTimeSec: Math.min(Math.max(0, videoTimeSec), finiteDuration),
shouldPlay: videoTimeSec >= 0 && videoTimeSec < finiteDuration,