Skip to content

Commit 9aa6346

Browse files
fix(studio): audio keeps playing after a timeline move or trim in a static composition (#6428)
A composition with no GSAP timeline was treated as zero length after an edit, which dropped preview audio, and an edit made during playback left the transport stopped. The runtime now keeps the declared length when it rebinds, and the soft reload resumes a transport that was playing.
1 parent 1428f87 commit 9aa6346

4 files changed

Lines changed: 72 additions & 6 deletions

File tree

‎packages/core/src/runtime/init.compositionLength.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ describe("runtime composition length and size", () => {
7171
expect(window.__player?.getDuration()).toBe(9);
7272
});
7373

74+
it("keeps a timeline-less composition's declared length when a rebind finds no timeline", () => {
75+
const html = `<div data-composition-id="main" data-root="true" data-duration="6"></div>`;
76+
expect(lengthOf(html, {})).toBe(6);
77+
78+
window.__hfForceTimelineRebind?.();
79+
80+
expect(window.__player?.getDuration()).toBe(6);
81+
window.__player?.play();
82+
expect(window.__player?.isPlaying()).toBe(true);
83+
});
84+
85+
it("reports 0 after a rebind for a timeline-less composition with nothing that gives a length", () => {
86+
expect(lengthOf(`<div data-composition-id="main" data-root="true"></div>`, {})).toBe(0);
87+
88+
window.__hfForceTimelineRebind?.();
89+
90+
expect(window.__player?.getDuration()).toBe(0);
91+
});
92+
7493
it("takes the timeline's length when the root declares none", () => {
7594
const html = `<div data-composition-id="main" data-root="true"></div>`;
7695
expect(lengthOf(html, { main: createMockTimeline(6) })).toBe(6);

‎packages/core/src/runtime/init.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,7 @@ export function initSandboxRuntimeModular(): void {
22932293
// A successful clear must not leave the player seeking a killed timeline.
22942294
state.capturedTimeline = null;
22952295
childrenBound = false;
2296-
clock.setDuration(0);
2296+
clock.setDuration(getSafeTimelineDurationSeconds(null, 0));
22972297
syncTimedElementVisibility(state.currentTime);
22982298
return;
22992299
}
@@ -4676,10 +4676,6 @@ export function initSandboxRuntimeModular(): void {
46764676
state.currentTime = start;
46774677
seekTimelineAndAdapters(start);
46784678
}
4679-
} else {
4680-
const rootEl = resolveRootCompositionElement();
4681-
const declaredDur = Number(rootEl?.getAttribute("data-duration") ?? 0);
4682-
if (declaredDur > 0) clock.setDuration(declaredDur);
46834679
}
46844680
pauseTimelineIfPossible(tl);
46854681
if (!clock.play()) return;

‎packages/studio/src/utils/gsapSoftReload.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,49 @@ describe("applySoftReloadFinalization", () => {
379379
expect(contentWindow.__timelines.root).toBe(mockTimeline);
380380
});
381381

382+
describe("play state", () => {
383+
function playerThatLogs(playing: boolean) {
384+
const calls: string[] = [];
385+
const player = {
386+
getTime: () => 2.0,
387+
isPlaying: () => playing,
388+
seek: vi.fn(() => calls.push("seek")),
389+
play: vi.fn(() => calls.push("play")),
390+
};
391+
const built = buildMockIframe({
392+
__player: player,
393+
__hfForceTimelineRebind: vi.fn(() => calls.push("rebind")),
394+
__hfStudioManualEditsApply: vi.fn(() => calls.push("edits")),
395+
});
396+
return { ...built, player, calls };
397+
}
398+
399+
it("resumes playing once the timeline is rebuilt when an edit lands during playback", () => {
400+
const { iframe, calls } = playerThatLogs(true);
401+
402+
expect(applySoftReloadFinalization(iframe, 2.0)).toBe(true);
403+
404+
expect(calls).toEqual(["seek", "rebind", "edits", "play"]);
405+
});
406+
407+
it("leaves a paused player paused", () => {
408+
const { iframe, player } = playerThatLogs(false);
409+
410+
expect(applySoftReloadFinalization(iframe, 2.0)).toBe(true);
411+
412+
expect(player.seek).toHaveBeenCalledWith(2.0);
413+
expect(player.play).not.toHaveBeenCalled();
414+
});
415+
416+
it("resumes after a script soft reload too, which shares the finalization", () => {
417+
const { iframe, calls } = playerThatLogs(true);
418+
419+
expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("applied");
420+
421+
expect(calls.at(-1)).toBe("play");
422+
});
423+
});
424+
382425
it("runs inside __hfSuppressSceneMutations when the runtime provides it", () => {
383426
const suppress = vi.fn(<T>(fn: () => T): T => fn());
384427
const { iframe, contentWindow } = buildMockIframe({

‎packages/studio/src/utils/gsapSoftReload.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212

1313
type IframeWindow = Window & {
1414
__timelines?: Record<string, { kill?: () => void; pause?: () => void }>;
15-
__player?: { getTime?: () => number; seek?: (t: number) => void };
15+
__player?: {
16+
getTime?: () => number;
17+
seek?: (t: number) => void;
18+
isPlaying?: () => boolean;
19+
play?: () => void;
20+
};
1621
__hfForceTimelineRebind?: () => void;
1722
__hfSuppressSceneMutations?: <T>(fn: () => T) => T;
1823
__hfStudioManualEditsApply?: () => void;
@@ -252,9 +257,12 @@ export interface SoftReloadOptions {
252257
* no-op if the timeline already reports being at that time internally.
253258
*/
254259
function finalizeSoftReload(win: IframeWindow, currentTime: number): void {
260+
// A seek pauses the player, so an edit made while playing resumes it once the timeline is rebuilt.
261+
const wasPlaying = win.__player?.isPlaying?.() === true;
255262
win.__player?.seek?.(currentTime);
256263
win.__hfForceTimelineRebind?.();
257264
win.__hfStudioManualEditsApply?.();
265+
if (wasPlaying) win.__player?.play?.();
258266
}
259267

260268
/**

0 commit comments

Comments
 (0)