Skip to content

Production-ready animations, transitions, backgrounds, and scenes for Remotion

fix: autoplay

dev_wandryfd59a4c26bf3
diff --git a/app/(home)/components/sections/bento-registry.tsx b/app/(home)/components/sections/bento-registry.tsx
index 8bf7a800..61a68405 100644
--- a/app/(home)/components/sections/bento-registry.tsx
+++ b/app/(home)/components/sections/bento-registry.tsx
@@ -4,7 +4,7 @@ import { Player, type PlayerRef } from "@remotion/player";
import { ArrowRight, Check, Copy } from "lucide-react";
import { motion } from "motion/react";
import Link from "next/link";
-import { useEffect, useRef, useState } from "react";
+import { useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import { SPRING_SOFT } from "@/config/site";
import { useTrackEvent } from "@/lib/analytics";
@@ -13,6 +13,7 @@ import registry from "@/registry/__index__";
import { SpotlightSurface } from "@/components/spotlight-surface";
import { FadeUp } from "../fade-up";
import { SectionHeading } from "../section-heading";
+import { useAutoplay } from "../use-autoplay";
/** Copyable `npx shadcn add` pill shown in each card footer. */
function InstallPill({ name }: { name: string }) {
@@ -80,27 +81,7 @@ function BentoCard({
const entry = registry[name];
const playerRef = useRef<PlayerRef>(null);
- // Reliable autoplay — `<Player autoPlay>` mounts a tick before its imperative
- // handle is ready (worse under Strict Mode's dev double-mount) and silently
- // stalls; mount paused and drive play() via the ref on the next animation
- // frame with a one-shot retry. Mirrors PreviewStage / use-player-controls.ts.
- useEffect(() => {
- if (!entry) return;
- let raf1 = 0;
- let raf2 = 0;
- raf1 = requestAnimationFrame(() => {
- playerRef.current?.play();
- raf2 = requestAnimationFrame(() => {
- if (playerRef.current && !playerRef.current.isPlaying()) {
- playerRef.current.play();
- }
- });
- });
- return () => {
- if (raf1) cancelAnimationFrame(raf1);
- if (raf2) cancelAnimationFrame(raf2);
- };
- }, [entry]);
+ useAutoplay(playerRef, Boolean(entry));
return (
<motion.div
@@ -143,6 +124,7 @@ function BentoCard({
compositionHeight={entry.config.compositionHeight}
style={{ width: "100%", height: "100%" }}
loop
+ initiallyMuted
acknowledgeRemotionLicense
/>
) : null}
diff --git a/app/(home)/components/sections/hero.tsx b/app/(home)/components/sections/hero.tsx
index 7bd88a6d..42410883 100644
--- a/app/(home)/components/sections/hero.tsx
+++ b/app/(home)/components/sections/hero.tsx
@@ -4,7 +4,7 @@ import { Player, type PlayerRef } from "@remotion/player";
import { ArrowRight, Pause, Play } from "lucide-react";
import { motion } from "motion/react";
import Link from "next/link";
-import { useCallback, useEffect, useRef, useState } from "react";
+import { useCallback, useRef, useState } from "react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { INSTALL_COMMAND, SPRING_BOUNCE } from "@/config/site";
@@ -13,6 +13,7 @@ import { HERO_CODE } from "@/lib/config/snippets";
import registry from "@/registry/__index__";
import { FadeUp } from "../fade-up";
import { InstallCommand } from "../install-command";
+import { useAutoplay } from "../use-autoplay";
export function Hero() {
const heroEntry = registry["glass-code-block"];
@@ -41,27 +42,7 @@ export function Hero() {
}
}, [trackEvent]);
- // Reliable autoplay — `<Player autoPlay>` mounts a tick before its imperative
- // handle is ready (worse under Strict Mode's dev double-mount) and silently
- // stalls (Pause UI, frozen frame). Mount paused and drive play() via the ref
- // on the next animation frame with a one-shot retry. Mirrors PreviewStage /
- // stars/hooks/use-player-controls.ts.
- useEffect(() => {
- let raf1 = 0;
- let raf2 = 0;
- raf1 = requestAnimationFrame(() => {
- playerRef.current?.play();
- raf2 = requestAnimationFrame(() => {
- if (playerRef.current && !playerRef.current.isPlaying()) {
- playerRef.current.play();
- }
- });
- });
- return () => {
- if (raf1) cancelAnimationFrame(raf1);
- if (raf2) cancelAnimationFrame(raf2);
- };
- }, []);
+ useAutoplay(playerRef);
const aspectRatio = heroEntry
? `${heroEntry.config.compositionWidth} / ${heroEntry.config.compositionHeight}`
@@ -174,6 +155,7 @@ export function Hero() {
compositionHeight={heroEntry.config.compositionHeight}
style={{ width: "100%", height: "100%", display: "block" }}
loop
+ initiallyMuted
acknowledgeRemotionLicense
/>
) : null}
diff --git a/app/(home)/components/sections/interactive-code.tsx b/app/(home)/components/sections/interactive-code.tsx
index 0106b623..93be070e 100644
--- a/app/(home)/components/sections/interactive-code.tsx
+++ b/app/(home)/components/sections/interactive-code.tsx
@@ -6,7 +6,6 @@ import {
type KeyboardEvent,
type PointerEvent,
useCallback,
- useEffect,
useMemo,
useRef,
useState,
@@ -18,6 +17,7 @@ import registry from "@/registry/__index__";
import { FadeUp } from "../fade-up";
import { SectionHeading } from "../section-heading";
import { SYNTAX_DARK, TypewriterCodeBlock } from "../typewriter-code-block";
+import { useAutoplay } from "../use-autoplay";
const COMPONENT = "typewriter";
@@ -256,27 +256,7 @@ export function InteractiveCode() {
const entry = registry[COMPONENT];
const playerRef = useRef<PlayerRef>(null);
- // Reliable autoplay — `<Player autoPlay>` mounts a tick before its imperative
- // handle is ready (worse under Strict Mode's dev double-mount) and silently
- // stalls; mount paused and drive play() via the ref on the next animation
- // frame with a one-shot retry. Mirrors PreviewStage / use-player-controls.ts.
- useEffect(() => {
- if (!entry) return;
- let raf1 = 0;
- let raf2 = 0;
- raf1 = requestAnimationFrame(() => {
- playerRef.current?.play();
- raf2 = requestAnimationFrame(() => {
- if (playerRef.current && !playerRef.current.isPlaying()) {
- playerRef.current.play();
- }
- });
- });
- return () => {
- if (raf1) cancelAnimationFrame(raf1);
- if (raf2) cancelAnimationFrame(raf2);
- };
- }, [entry]);
+ useAutoplay(playerRef, Boolean(entry));
const [text, setText] = useState(TYPEWRITER_DEFAULTS.text);
const [fontSize, setFontSize] = useState(TYPEWRITER_DEFAULTS.fontSize);
@@ -390,6 +370,7 @@ export function InteractiveCode() {
compositionHeight={entry.config.compositionHeight}
style={{ width: "100%", height: "100%", display: "block" }}
loop
+ initiallyMuted
acknowledgeRemotionLicense
/>
) : null}
diff --git a/app/(home)/components/use-autoplay.ts b/app/(home)/components/use-autoplay.ts
new file mode 100644
index 00000000..05640290
--- /dev/null
+++ b/app/(home)/components/use-autoplay.ts
@@ -0,0 +1,34 @@
+import type { PlayerRef } from "@remotion/player";
+import { type RefObject, useEffect } from "react";
+
+/**
+ * Reliable autoplay for a `<Player>`. The `autoPlay` prop is discouraged and
+ * programmatic `play()` is "not entirely deterministic" (Remotion docs) — the
+ * player can mount a tick before its imperative handle is ready, and on the
+ * landing page many Players mount at once, so a player often isn't ready within
+ * a frame or two. Mount the Player paused and drive `play()` here, retrying each
+ * animation frame until it actually reports playing (bounded so a genuinely
+ * blocked player can't spin forever). These compositions have no audio, so the
+ * browser autoplay policy permits programmatic playback.
+ */
+export function useAutoplay(
+ playerRef: RefObject<PlayerRef | null>,
+ enabled = true,
+) {
+ useEffect(() => {
+ if (!enabled) return;
+ let raf = 0;
+ let attempts = 0;
+ const MAX_ATTEMPTS = 120; // ~2s at 60fps, longer on a contended mount
+ const tick = () => {
+ const player = playerRef.current;
+ if (player && !player.isPlaying()) player.play();
+ attempts += 1;
+ if ((!player || !player.isPlaying()) && attempts < MAX_ATTEMPTS) {
+ raf = requestAnimationFrame(tick);
+ }
+ };
+ raf = requestAnimationFrame(tick);
+ return () => cancelAnimationFrame(raf);
+ }, [playerRef, enabled]);
+}