diff --git a/src/components/ai-edition/NewEditorShell.togglePlay.test.tsx b/src/components/ai-edition/NewEditorShell.togglePlay.test.tsx index 56ff0fc79..f00c8aafc 100644 --- a/src/components/ai-edition/NewEditorShell.togglePlay.test.tsx +++ b/src/components/ai-edition/NewEditorShell.togglePlay.test.tsx @@ -223,7 +223,7 @@ describe("NewEditorShell togglePlay cross-asset replay", () => { it.each([ true, false, - ])("keeps playback=%s when a manual seek crosses into another asset", async (playing) => { + ])("lets the preview resolve implicit seek intent when the transport is %s", async (playing) => { useProjectStore.setState({ document: makeCrossAssetDocument(), projectId: "p1", @@ -240,6 +240,8 @@ describe("NewEditorShell togglePlay cross-asset replay", () => { fireEvent.keyDown(document.body, { key: "ArrowRight", code: "ArrowRight" }); }); expect(mocks.latestSeekTarget?.timeSec).toBeGreaterThan(5); - expect(mocks.latestSeekTarget?.preservePlayback).toBe(playing); + // A replacement video briefly reports paused before metadata. The shell + // must leave an implicit seek to the preview's current or queued intent. + expect(mocks.latestSeekTarget?.preservePlayback).toBeUndefined(); }); }); diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx index 652564218..008255319 100644 --- a/src/components/ai-edition/NewEditorShell.tsx +++ b/src/components/ai-edition/NewEditorShell.tsx @@ -582,9 +582,9 @@ export function NewEditorShell() { timeSec, isSource: false, requestId: ++seekSeqRef.current, - // Timeline and transcript seeks omit the flag. Carry the transport's - // intent across a source swap, as a seek within the same video does. - preservePlayback: preservePlayback ?? useProjectStore.getState().playing, + // The preview owns queued playback while another source loads. Its new + // video is initially paused, so the store is not the scrub's intent. + preservePlayback, }); }, [setCurrentTime, canSeek], diff --git a/src/components/ai-edition/VirtualPreview.playback.test.tsx b/src/components/ai-edition/VirtualPreview.playback.test.tsx index 1d34b6e3b..405a89fb4 100644 --- a/src/components/ai-edition/VirtualPreview.playback.test.tsx +++ b/src/components/ai-edition/VirtualPreview.playback.test.tsx @@ -1163,6 +1163,105 @@ describe("VirtualPreview replay", () => { expect(reportedTimes(onTimeChange).at(-1)).toBe(14); }); + it.each([ + { + initiallyPlaying: true, + firstIntent: undefined, + nextIntent: undefined, + shouldPlay: true, + intermediateImplicitSeek: false, + }, + { + initiallyPlaying: true, + firstIntent: true, + nextIntent: undefined, + shouldPlay: true, + intermediateImplicitSeek: false, + }, + { + initiallyPlaying: true, + firstIntent: true, + nextIntent: false, + shouldPlay: false, + intermediateImplicitSeek: false, + }, + { + initiallyPlaying: false, + firstIntent: undefined, + nextIntent: undefined, + shouldPlay: false, + intermediateImplicitSeek: false, + }, + { + initiallyPlaying: true, + firstIntent: true, + nextIntent: false, + shouldPlay: false, + intermediateImplicitSeek: true, + }, + ])("keeps source-switch intent (playing $initiallyPlaying; intermediate $intermediateImplicitSeek; next $nextIntent)", ({ + initiallyPlaying, + firstIntent, + nextIntent, + shouldPlay, + intermediateImplicitSeek, + }) => { + const sources: VideoSource[] = [ + { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" }, + { id: "a2", src: "file:///tmp/a2.mp4", label: "a2" }, + ]; + const clips = [clip("clip_1", "a1", 0, 5, 0), clip("clip_2", "a2", 0, 5, 5)]; + let seekTarget: { + timeSec: number; + requestId: number; + preservePlayback?: boolean; + } | null = null; + const view = render( + , + ); + const firstElement = view.container.querySelector("video"); + if (!firstElement) throw new Error("no initial video"); + const firstVideo = driveVideo(firstElement); + act(() => fireEvent.loadedMetadata(firstElement)); + if (initiallyPlaying) firstVideo.play(); + firstVideo.seekTo(3); + + act(() => { + seekTarget = { timeSec: 6, requestId: 1, preservePlayback: firstIntent }; + view.rerender( + , + ); + }); + const nextElement = view.container.querySelector("video"); + if (!nextElement || nextElement === firstElement) throw new Error("source did not switch"); + const nextVideo = driveVideo(nextElement); + expect(nextElement.paused).toBe(true); + if (intermediateImplicitSeek) { + act(() => { + seekTarget = { timeSec: 6.25, requestId: 2 }; + view.rerender( + , + ); + }); + } + + // Pointerup or a later drag position arrives while the new decoder is + // still paused before metadata. It must retain the scrub's play intent. + act(() => { + seekTarget = { + timeSec: 6.5, + requestId: intermediateImplicitSeek ? 3 : 2, + preservePlayback: nextIntent, + }; + view.rerender( + , + ); + }); + act(() => fireEvent.loadedMetadata(nextElement)); + expect(nextVideo.currentTime).toBe(1.5); + expect(nextElement.paused).toBe(!shouldPlay); + }); + it("does not let stale loadState in tick closure block clip boundary checks or timeline end", () => { const sources: VideoSource[] = [ { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" }, diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx index eb79d6f04..d3ad133fc 100644 --- a/src/components/ai-edition/VirtualPreview.tsx +++ b/src/components/ai-edition/VirtualPreview.tsx @@ -1523,7 +1523,7 @@ export function VirtualPreview({ }, []); const seekToVirtualTime = useCallback( - (nextVirtualTimeSec: number, preservePlayback = false, forceResume = false) => { + (nextVirtualTimeSec: number, preservePlayback?: boolean, forceResume = false) => { const position = locateVirtualPosition(clips, nextVirtualTimeSec); if (!position) { videoRef.current?.pause(); @@ -1553,7 +1553,14 @@ export function VirtualPreview({ // race against the rAF tick's own boundary check: the native `ended` // event fires first, stops playback outright, and the multi-clip // timeline never advances — the "stops at clip end" bug. - const shouldContinuePlayback = preservePlayback && (forceResume || !videoRef.current?.paused); + // A scrub can publish again before the replacement video's metadata + // arrives. That element starts paused; keep the queued intent instead of + // turning pointerup or the next drag position into a request to stop. + // An explicit false still cancels the queued resume. + const shouldContinuePlayback = + preservePlayback === undefined + ? (pendingSeekRef.current?.play ?? !videoRef.current?.paused) + : preservePlayback && (forceResume || !videoRef.current?.paused); if (isAssetSwitch) { if (!shouldContinuePlayback) { @@ -1583,6 +1590,10 @@ export function VirtualPreview({ sourceTimeSec: position.sourceTimeSec, play: shouldContinuePlayback, }; + updateVirtualTime(position.virtualTimeSec); + // Metadata owns the pending seek and resume. Calling play now would + // queue a browser autoplay that a later paused seek cannot cancel. + return; } updateVirtualTime(position.virtualTimeSec); applySourceTime(video, position.sourceTimeSec);