Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

fix(editor): resolve local audio sources for editor playback

webadderall3a73e899a7d4
diff --git a/src/components/video-editor/VideoEditor.tsx b/src/components/video-editor/VideoEditor.tsx
index 83ed952a..f70e46f7 100644
--- a/src/components/video-editor/VideoEditor.tsx
+++ b/src/components/video-editor/VideoEditor.tsx
@@ -42,6 +42,7 @@ import {
type SupportedMp4Dimensions,
VideoExporter,
} from "@/lib/exporter";
+import { resolveMediaElementSource } from "@/lib/exporter/localMediaSource";
import { matchesShortcut } from "@/lib/shortcuts";
import { type AspectRatio, getAspectRatioValue } from "@/utils/aspectRatioUtils";
import { resolveAutoCaptionSourcePath } from "./autoCaptionSource";
@@ -2371,8 +2372,11 @@ export default function VideoEditor() {
// Audio playback sync: manage Audio elements that play in sync with video
const audioElementsRef = useRef<Map<string, HTMLAudioElement>>(new Map());
+ const audioElementRevokersRef = useRef<Map<string, () => void>>(new Map());
+ const audioElementResourcesRef = useRef<Map<string, string>>(new Map());
useEffect(() => {
+ let cancelled = false;
const existing = audioElementsRef.current;
const currentIds = new Set(audioRegions.map((r) => r.id));
@@ -2381,6 +2385,9 @@ export default function VideoEditor() {
if (!currentIds.has(id)) {
audio.pause();
audio.src = "";
+ audioElementRevokersRef.current.get(id)?.();
+ audioElementRevokersRef.current.delete(id);
+ audioElementResourcesRef.current.delete(id);
existing.delete(id);
}
}
@@ -2393,21 +2400,54 @@ export default function VideoEditor() {
audio.preload = "auto";
existing.set(region.id, audio);
}
- const expectedSrc = toFileUrl(region.audioPath);
- if (audio.src !== expectedSrc) {
- audio.src = expectedSrc;
+
+ if (audioElementResourcesRef.current.get(region.id) !== region.audioPath) {
+ audio.pause();
+ audio.src = "";
+ audioElementRevokersRef.current.get(region.id)?.();
+ audioElementRevokersRef.current.delete(region.id);
+ audioElementResourcesRef.current.set(region.id, region.audioPath);
+
+ void (async () => {
+ const resolved = await resolveMediaElementSource(region.audioPath);
+ const latestAudio = existing.get(region.id);
+
+ if (
+ cancelled ||
+ latestAudio !== audio ||
+ audioElementResourcesRef.current.get(region.id) !== region.audioPath
+ ) {
+ resolved.revoke();
+ return;
+ }
+
+ audioElementRevokersRef.current.set(region.id, resolved.revoke);
+ latestAudio.src = resolved.src;
+ })();
}
+
audio.volume = Math.max(0, Math.min(1, region.volume * previewVolume));
}
return () => {
- for (const audio of existing.values()) {
+ cancelled = true;
+ };
+ }, [audioRegions, previewVolume]);
+
+ useEffect(() => {
+ return () => {
+ for (const audio of audioElementsRef.current.values()) {
audio.pause();
audio.src = "";
}
- existing.clear();
+ for (const revoke of audioElementRevokersRef.current.values()) {
+ revoke();
+ }
+ audioElementsRef.current.clear();
+ audioElementRevokersRef.current.clear();
+ audioElementResourcesRef.current.clear();
};
- }, [audioRegions, previewVolume]);
+ }, []);
// Sync audio playback with video currentTime and isPlaying state
useEffect(() => {
diff --git a/src/components/video-editor/timeline/TimelineEditor.tsx b/src/components/video-editor/timeline/TimelineEditor.tsx
index a15ccde6..72c6dc53 100644
--- a/src/components/video-editor/timeline/TimelineEditor.tsx
+++ b/src/components/video-editor/timeline/TimelineEditor.tsx
@@ -14,6 +14,7 @@ import { v4 as uuidv4 } from 'uuid';
import { useScopedT } from "@/contexts/I18nContext";
import { useShortcuts } from "@/contexts/ShortcutsContext";
import { matchesShortcut } from "@/lib/shortcuts";
+import { resolveMediaElementSource } from "@/lib/exporter/localMediaSource";
import { ASPECT_RATIOS, type AspectRatio, getAspectRatioLabel, isCustomAspectRatio } from "@/utils/aspectRatioUtils";
import { formatShortcut } from "@/utils/platformUtils";
import { loadEditorPreferences, saveEditorPreferences } from "../editorPreferences";
@@ -24,7 +25,6 @@ import Item from "./Item";
import KeyframeMarkers from "./KeyframeMarkers";
import type { Range, Span } from "dnd-timeline";
import type { ZoomRegion, TrimRegion, AnnotationRegion, SpeedRegion, AudioRegion, CursorTelemetryPoint, ZoomFocus } from "../types";
-import { toFileUrl } from "../projectPersistence";
import { detectInteractionCandidates, normalizeCursorTelemetry } from "./zoomSuggestionUtils";
const ZOOM_ROW_ID = "row-zoom";
@@ -1196,14 +1196,24 @@ export default function TimelineEditor({
const audioPath = result.path;
// Load the audio file to get its full duration
+ const audioSource = await resolveMediaElementSource(audioPath);
const audioDurationMs = await new Promise<number>((resolve) => {
- const audio = new Audio(toFileUrl(audioPath));
+ const audio = new Audio();
+ const finish = (durationMs: number) => {
+ audio.pause();
+ audio.src = "";
+ audioSource.revoke();
+ resolve(durationMs);
+ };
+
+ audio.preload = "metadata";
audio.addEventListener('loadedmetadata', () => {
- resolve(Math.round(audio.duration * 1000));
- });
+ finish(Math.round(audio.duration * 1000));
+ }, { once: true });
audio.addEventListener('error', () => {
- resolve(0);
- });
+ finish(0);
+ }, { once: true });
+ audio.src = audioSource.src;
});
if (audioDurationMs <= 0) {