Open source CapCut alternative.
TypeScript
fix(site): route audio previews through one shared player
David Le85a793f7b6a7
diff --git a/site/src/cut/components/AudioPanel.tsx b/site/src/cut/components/AudioPanel.tsxindex 83e23908..261fede5 100644--- a/site/src/cut/components/AudioPanel.tsx+++ b/site/src/cut/components/AudioPanel.tsx@@ -36,6 +36,7 @@ import {} from "@/cut/lib/library";import { creditsUrl, signInUrl, useSignedIn } from "@/cut/lib/generate";import { enrichAsset } from "@/cut/lib/media";+import { usePreviewAudio } from "@/cut/lib/previewAudio";import { useEditor } from "@/cut/lib/store";import { formatTime } from "@/cut/lib/time";import { NoCreditsError, synthesizeSpeech } from "@/cut/lib/tts";@@ -67,25 +68,13 @@ export function AudioPanel({projectId: string;importing: boolean;}) {- // One shared player for every row so starting a clip stops the last one.- const player = useRef<HTMLAudioElement | null>(null);- const [playingUrl, setPlayingUrl] = useState<string | null>(null);- const togglePlay = (url: string) => {- const el = (player.current ??= new Audio());- if (playingUrl === url) {- el.pause();- setPlayingUrl(null);- return;- }- el.src = url;- el.onended = () => setPlayingUrl(null);- el.onerror = () => setPlayingUrl(null);- setPlayingUrl(url);- void el.play().catch(() => setPlayingUrl(null));- };+ // The app-wide preview player: starting a clip stops the last one, here or+ // on a chat audio card. Leaving the tab stops whatever is playing.+ const playingUrl = usePreviewAudio((s) => s.url);+ const togglePlay = usePreviewAudio((s) => s.toggle);useEffect(() => () => {- player.current?.pause();+ usePreviewAudio.getState().stop();},[]);diff --git a/site/src/cut/lib/previewAudio.ts b/site/src/cut/lib/previewAudio.tsnew file mode 100644index 00000000..56b4310f--- /dev/null+++ b/site/src/cut/lib/previewAudio.ts@@ -0,0 +1,42 @@+"use client";++import { create } from "zustand";++// One shared preview player for every "play this audio" affordance — Audio+// panel rows and chat audio cards. Starting a preview stops the last one, and+// every surface reads the same playing-url state, so previews never stack.++interface PreviewAudioState {+ /** URL of the preview currently playing, or null. */+ url: string | null;+ /** Play `url`, stopping whatever else was playing; pause when it is already+ * the one playing. */+ toggle: (url: string) => void;+ /** Stop the preview — entirely, or only when it is playing `url` (a surface+ * going away should silence its own preview, not someone else's). */+ stop: (url?: string) => void;+}++let el: HTMLAudioElement | null = null;++export const usePreviewAudio = create<PreviewAudioState>((set, get) => ({+ url: null,+ toggle: (url) => {+ const audio = (el ??= new Audio());+ if (get().url === url) {+ audio.pause();+ set({ url: null });+ return;+ }+ audio.src = url;+ audio.onended = () => set({ url: null });+ audio.onerror = () => set({ url: null });+ set({ url });+ void audio.play().catch(() => set({ url: null }));+ },+ stop: (url) => {+ if (url !== undefined && get().url !== url) return;+ el?.pause();+ set({ url: null });+ },+}));