Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

fix(audio): sync audio playback position with timeline seek (#7)

Augustus Otuea2ebaffae6c
diff --git a/apps/web/src/components/editor/Preview.tsx b/apps/web/src/components/editor/Preview.tsx
index 6ac1f06c..ee8f8c39 100644
--- a/apps/web/src/components/editor/Preview.tsx
+++ b/apps/web/src/components/editor/Preview.tsx
@@ -1889,6 +1889,7 @@ export const Preview: React.FC = () => {
await audioGraph.resume();
audioGraph.seekTo(startPosition);
+ await masterClock.play();
audioGraph.startScheduler(() => {
const tracksWithAudio = timelineTracks.filter(
(t) => (t.type === "audio" || t.type === "video") && !t.hidden,
@@ -1924,8 +1925,6 @@ export const Preview: React.FC = () => {
return schedules;
});
- await masterClock.play();
-
let isActive = true;
let rafId: number | null = null;
let currentClipId: string | null = null;
@@ -2855,9 +2854,8 @@ export const Preview: React.FC = () => {
masterClock.seek(playbackStartPosition);
audioGraph.seekTo(playbackStartPosition);
- audioGraph.startScheduler(getAudioClipsForScheduler);
-
await masterClock.play();
+ audioGraph.startScheduler(getAudioClipsForScheduler);
const frameDuration = 1000 / 30;
let lastFrameTimestamp = performance.now();
diff --git a/packages/core/src/audio/realtime-audio-graph.ts b/packages/core/src/audio/realtime-audio-graph.ts
index 07dfd61b..08a9db76 100644
--- a/packages/core/src/audio/realtime-audio-graph.ts
+++ b/packages/core/src/audio/realtime-audio-graph.ts
@@ -73,6 +73,7 @@ export class RealtimeAudioGraph {
private impulseResponseCache: Map<string, AudioBuffer> = new Map();
private isPlaying = false;
private lastScheduledTime = 0;
+ private seekPending = false;
private scheduleAheadTime = 0.2;
private schedulerIntervalId: number | null = null;
@@ -583,7 +584,10 @@ export class RealtimeAudioGraph {
if (this.schedulerIntervalId !== null) return;
this.isPlaying = true;
- this.lastScheduledTime = this.masterClock.currentTime;
+ if (!this.seekPending) {
+ this.lastScheduledTime = this.masterClock.currentTime;
+ }
+ this.seekPending = false;
const scheduleAudio = () => {
if (!this.isPlaying) return;
@@ -628,6 +632,7 @@ export class RealtimeAudioGraph {
seekTo(time: number): void {
this.stopAllClips();
this.lastScheduledTime = time;
+ this.seekPending = true;
}
dispose(): void {
diff --git a/packages/core/src/playback/master-timeline-clock.ts b/packages/core/src/playback/master-timeline-clock.ts
index db07ec0f..d6cb420b 100644
--- a/packages/core/src/playback/master-timeline-clock.ts
+++ b/packages/core/src/playback/master-timeline-clock.ts
@@ -40,8 +40,8 @@ export class MasterTimelineClock {
}
get currentTime(): number {
- if (this.state === "stopped") return 0;
- if (this.state === "paused") return this.pausedAt;
+ if (this.state === "stopped" || this.state === "paused")
+ return this.pausedAt;
const elapsed =
(this.audioContext.currentTime - this.startAudioContextTime) *