Skip to content

🎥 Make videos programmatically with React

docusmentation article: displaying the current time

Jonny Burger6b9da9018337
diff --git a/packages/docs/docs/player/current-time.md b/packages/docs/docs/player/current-time.md
new file mode 100644
index 0000000000..41d463639b
--- /dev/null
+++ b/packages/docs/docs/player/current-time.md
@@ -0,0 +1,132 @@
+---
+image: /generated/articles-docs-player-current-time.png
+id: current-time
+sidebar_label: "Getting the current time"
+title: "Displaying the current time"
+slug: /player/current-time
+crumb: "@remotion/player"
+---
+
+When rendering a [`<Player>`](/docs/player) in your app, special considerations must be taken to prevent constant re-renders of the app or [`<Player>`](/docs/player) if the time changes.
+
+This is why the [`useCurrentFrame()`](/docs/use-current-frame) hook does not work outside a composition.
+
+## Synchronizing a component with the Player time
+
+If you want to display a component that synchronizes with the time of the player, for example the cursor of a timeline component or a custom time display, you can use the following hook:
+
+```tsx twoslash title="use-current-player-frame.ts"
+import { CallbackListener, PlayerRef } from "@remotion/player";
+import React, { useSyncExternalStore } from "react";
+
+export const useCurrentPlayerFrame = (ref: React.RefObject<PlayerRef>) => {
+ const { current } = ref;
+
+ const data = useSyncExternalStore<number>(
+ (onStoreChange: (newVal: number) => void) => {
+ if (!current) {
+ return () => undefined;
+ }
+ const updater: CallbackListener<"frameupdate"> = ({ detail }) => {
+ onStoreChange(detail.frame);
+ };
+
+ current.addEventListener("frameupdate", updater);
+ return () => {
+ current.removeEventListener("frameupdate", updater);
+ };
+ },
+ () => ref.current?.getCurrentFrame() ?? 0,
+ () => 0
+ );
+
+ return data;
+};
+```
+
+## Usage example
+
+Add a ref to a React Player and pass it to another component:
+
+```tsx twoslash
+// @allowUmdGlobalAccess
+// @filename: ./remotion/MyVideo.tsx
+export const MyVideo = () => <></>;
+// @filename: ./remotion/TimeDisplay.tsx
+export const TimeDisplay: React.FC<{
+ playerRef: React.RefObject<PlayerRef>;
+}> = () => <></>;
+
+// @filename: index.tsx
+// ---cut---
+import { Player, PlayerRef } from "@remotion/player";
+import { useRef } from "react";
+import { MyVideo } from "./remotion/MyVideo";
+import { TimeDisplay } from "./remotion/TimeDisplay";
+
+export const App: React.FC = () => {
+ const playerRef = useRef<PlayerRef>(null);
+
+ return (
+ <>
+ <Player
+ ref={playerRef}
+ component={MyVideo}
+ durationInFrames={120}
+ compositionWidth={1920}
+ compositionHeight={1080}
+ fps={30}
+ />
+ <TimeDisplay playerRef={playerRef} />
+ </>
+ );
+};
+```
+
+This is how a component could access the current time:
+
+```tsx twoslash title="TimeDisplay.tsx"
+// @filename: ./use-current-player-frame.ts
+import { CallbackListener, PlayerRef } from "@remotion/player";
+import React, { useSyncExternalStore } from "react";
+
+export const useCurrentPlayerFrame = (ref: React.RefObject<PlayerRef>) => {
+ const { current } = ref;
+
+ const data = useSyncExternalStore<number>(
+ (onStoreChange: (newVal: number) => void) => {
+ if (!current) {
+ return () => undefined;
+ }
+ const updater: CallbackListener<"frameupdate"> = ({ detail }) => {
+ onStoreChange(detail.frame);
+ };
+
+ current.addEventListener("frameupdate", updater);
+ return () => {
+ current.removeEventListener("frameupdate", updater);
+ };
+ },
+ () => ref.current?.getCurrentFrame() ?? 0,
+ () => 0
+ );
+
+ return data;
+};
+
+// @filename: TimeDisplay.tsx
+// ---cut---
+import { useCurrentPlayerFrame } from "./use-current-player-frame";
+import type {PlayerRef} from '@remotion/player'
+import React from 'react';
+
+export const TimeDisplay: React.FC<{
+ playerRef: React.RefObject<PlayerRef>;
+}> = ({ playerRef }) => {
+ const frame = useCurrentPlayerFrame(playerRef);
+
+ return <div>current frame: {frame}</div>;
+};
+```
+
+This approach is efficient, because only the video itself and the component relying on the time are re-rendering, but the `<App>` component is not.
\ No newline at end of file
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 0432894831..7f5cab4804 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -340,6 +340,7 @@ module.exports = {
"player/scaling",
"player/integration",
"player/autoplay",
+ "player/current-time",
"player/preloading",
"player/best-practices",
],
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index f014e3edd8..2074947b8c 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1163,6 +1163,13 @@ export const articles = [
compId: "articles-docs-player-best-practices",
crumb: "@remotion/player",
},
+ {
+ id: "current-time",
+ title: "Displaying the current time",
+ relativePath: "docs/player/current-time.md",
+ compId: "articles-docs-player-current-time",
+ crumb: "@remotion/player",
+ },
{
id: "player/index",
title: "@remotion/player",
diff --git a/packages/docs/static/generated/articles-docs-player-current-time.png b/packages/docs/static/generated/articles-docs-player-current-time.png
new file mode 100644
index 0000000000..8b81302f75
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-player-current-time.png differ
diff --git a/packages/player-example/src/TimeDisplay.tsx b/packages/player-example/src/TimeDisplay.tsx
index 318ae25e70..f9afd5890c 100644
--- a/packages/player-example/src/TimeDisplay.tsx
+++ b/packages/player-example/src/TimeDisplay.tsx
@@ -1,6 +1,6 @@
import {PlayerRef} from '@remotion/player';
import React from 'react';
-import {useCurrentPlayerFrame} from './use-current-frame';
+import {useCurrentPlayerFrame} from './use-current-player-frame';
export const TimeDisplay: React.FC<{
playerRef: React.RefObject<PlayerRef>;
diff --git a/packages/player-example/src/use-current-frame.ts b/packages/player-example/src/use-current-player-frame.ts
similarity index 100%
rename from packages/player-example/src/use-current-frame.ts
rename to packages/player-example/src/use-current-player-frame.ts