Skip to content

Commit efe2373

Browse files
committed
fix(preview): keep scrubs playing while a new source loads
1 parent fccdcdf commit efe2373

4 files changed

Lines changed: 71 additions & 7 deletions

File tree

‎src/components/ai-edition/NewEditorShell.togglePlay.test.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ describe("NewEditorShell togglePlay cross-asset replay", () => {
223223
it.each([
224224
true,
225225
false,
226-
])("keeps playback=%s when a manual seek crosses into another asset", async (playing) => {
226+
])("lets the preview resolve implicit seek intent when the transport is %s", async (playing) => {
227227
useProjectStore.setState({
228228
document: makeCrossAssetDocument(),
229229
projectId: "p1",
@@ -240,6 +240,8 @@ describe("NewEditorShell togglePlay cross-asset replay", () => {
240240
fireEvent.keyDown(document.body, { key: "ArrowRight", code: "ArrowRight" });
241241
});
242242
expect(mocks.latestSeekTarget?.timeSec).toBeGreaterThan(5);
243-
expect(mocks.latestSeekTarget?.preservePlayback).toBe(playing);
243+
// A replacement video briefly reports paused before metadata. The shell
244+
// must leave an implicit seek to the preview's current or queued intent.
245+
expect(mocks.latestSeekTarget?.preservePlayback).toBeUndefined();
244246
});
245247
});

‎src/components/ai-edition/NewEditorShell.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@ export function NewEditorShell() {
582582
timeSec,
583583
isSource: false,
584584
requestId: ++seekSeqRef.current,
585-
// Timeline and transcript seeks omit the flag. Carry the transport's
586-
// intent across a source swap, as a seek within the same video does.
587-
preservePlayback: preservePlayback ?? useProjectStore.getState().playing,
585+
// The preview owns queued playback while another source loads. Its new
586+
// video is initially paused, so the store is not the scrub's intent.
587+
preservePlayback,
588588
});
589589
},
590590
[setCurrentTime, canSeek],

‎src/components/ai-edition/VirtualPreview.playback.test.tsx‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,61 @@ describe("VirtualPreview replay", () => {
11631163
expect(reportedTimes(onTimeChange).at(-1)).toBe(14);
11641164
});
11651165

1166+
it.each([
1167+
{ initiallyPlaying: true, firstIntent: undefined, nextIntent: undefined, shouldPlay: true },
1168+
{ initiallyPlaying: true, firstIntent: true, nextIntent: undefined, shouldPlay: true },
1169+
{ initiallyPlaying: true, firstIntent: true, nextIntent: false, shouldPlay: false },
1170+
{ initiallyPlaying: false, firstIntent: undefined, nextIntent: undefined, shouldPlay: false },
1171+
])("keeps a source-switch scrub playing unless explicitly paused: $firstIntent → $nextIntent", ({
1172+
initiallyPlaying,
1173+
firstIntent,
1174+
nextIntent,
1175+
shouldPlay,
1176+
}) => {
1177+
const sources: VideoSource[] = [
1178+
{ id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },
1179+
{ id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },
1180+
];
1181+
const clips = [clip("clip_1", "a1", 0, 5, 0), clip("clip_2", "a2", 0, 5, 5)];
1182+
let seekTarget: {
1183+
timeSec: number;
1184+
requestId: number;
1185+
preservePlayback?: boolean;
1186+
} | null = null;
1187+
const view = render(
1188+
<VirtualPreview videoSources={sources} clips={clips} seekTarget={seekTarget} />,
1189+
);
1190+
const firstElement = view.container.querySelector("video");
1191+
if (!firstElement) throw new Error("no initial video");
1192+
const firstVideo = driveVideo(firstElement);
1193+
act(() => fireEvent.loadedMetadata(firstElement));
1194+
if (initiallyPlaying) firstVideo.play();
1195+
firstVideo.seekTo(3);
1196+
1197+
act(() => {
1198+
seekTarget = { timeSec: 6, requestId: 1, preservePlayback: firstIntent };
1199+
view.rerender(
1200+
<VirtualPreview videoSources={sources} clips={clips} seekTarget={seekTarget} />,
1201+
);
1202+
});
1203+
const nextElement = view.container.querySelector("video");
1204+
if (!nextElement || nextElement === firstElement) throw new Error("source did not switch");
1205+
const nextVideo = driveVideo(nextElement);
1206+
expect(nextElement.paused).toBe(true);
1207+
1208+
// Pointerup or a later drag position arrives while the new decoder is
1209+
// still paused before metadata. It must retain the scrub's play intent.
1210+
act(() => {
1211+
seekTarget = { timeSec: 6.5, requestId: 2, preservePlayback: nextIntent };
1212+
view.rerender(
1213+
<VirtualPreview videoSources={sources} clips={clips} seekTarget={seekTarget} />,
1214+
);
1215+
});
1216+
act(() => fireEvent.loadedMetadata(nextElement));
1217+
expect(nextVideo.currentTime).toBe(1.5);
1218+
expect(nextElement.paused).toBe(!shouldPlay);
1219+
});
1220+
11661221
it("does not let stale loadState in tick closure block clip boundary checks or timeline end", () => {
11671222
const sources: VideoSource[] = [
11681223
{ id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },

‎src/components/ai-edition/VirtualPreview.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ export function VirtualPreview({
15351535
}, []);
15361536

15371537
const seekToVirtualTime = useCallback(
1538-
(nextVirtualTimeSec: number, preservePlayback = false, forceResume = false) => {
1538+
(nextVirtualTimeSec: number, preservePlayback?: boolean, forceResume = false) => {
15391539
const position = locateVirtualPosition(clips, nextVirtualTimeSec);
15401540
if (!position) {
15411541
videoRef.current?.pause();
@@ -1565,7 +1565,14 @@ export function VirtualPreview({
15651565
// race against the rAF tick's own boundary check: the native `ended`
15661566
// event fires first, stops playback outright, and the multi-clip
15671567
// timeline never advances — the "stops at clip end" bug.
1568-
const shouldContinuePlayback = preservePlayback && (forceResume || !videoRef.current?.paused);
1568+
// A scrub can publish again before the replacement video's metadata
1569+
// arrives. That element starts paused; keep the queued intent instead of
1570+
// turning pointerup or the next drag position into a request to stop.
1571+
// An explicit false still cancels the queued resume.
1572+
const shouldContinuePlayback =
1573+
preservePlayback === undefined
1574+
? (pendingSeekRef.current?.play ?? !videoRef.current?.paused)
1575+
: preservePlayback && (forceResume || !videoRef.current?.paused);
15691576

15701577
if (isAssetSwitch) {
15711578
if (!shouldContinuePlayback) {

0 commit comments

Comments
 (0)