Skip to content

🎥 Make videos programmatically with React

Add play button to docs to counteract autoplay restriction, fixes #3618

Jonny Burger8437b017a4c6
diff --git a/packages/docs/components/DocsDark.tsx b/packages/docs/components/DocsDark.tsx
index 81cdb3bd4b..c606bf9d3c 100644
--- a/packages/docs/components/DocsDark.tsx
+++ b/packages/docs/components/DocsDark.tsx
@@ -1,6 +1,13 @@
import { useColorMode } from "@docusaurus/theme-common";
import { PlayerInternals } from "@remotion/player";
-import React, { useEffect, useMemo, useRef } from "react";
+import React, {
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
+import { AbsoluteFill } from "remotion";
import { Spacer } from "./layout/Spacer";
export const SingleVideoDemo: React.FC<{
@@ -35,7 +42,7 @@ export const SingleVideoDemo: React.FC<{
useEffect(() => {
ref1.current.addEventListener(
- "progress",
+ "canplay",
() => {
ref1.current.play();
},
@@ -68,6 +75,7 @@ export const DualVideoDemo: React.FC<{
rightLight: string;
}> = ({ leftDark, leftLight, rightDark, rightLight }) => {
const { colorMode } = useColorMode();
+ const [played, setPlayed] = useState(false);
const container = useRef<HTMLDivElement>(null);
const ref1 = useRef<HTMLVideoElement>(null);
@@ -100,9 +108,16 @@ export const DualVideoDemo: React.FC<{
overflow: "hidden",
borderRadius: "var(--ifm-code-border-radius)",
aspectRatio: "16/9",
+ position: "relative",
};
}, [colorMode, mobile]);
+ const playAll = useCallback(() => {
+ ref1.current.play();
+ ref2.current.play();
+ setPlayed(true);
+ }, []);
+
useEffect(() => {
Promise.all([
new Promise<void>((resolve) => {
@@ -116,22 +131,60 @@ export const DualVideoDemo: React.FC<{
});
}),
]).then(() => {
- ref1.current.play();
- ref2.current.play();
+ playAll();
});
- }, [colorMode]);
+ }, [colorMode, playAll]);
return (
<div ref={container} style={style}>
<div style={videoContainer}>
- <video
- ref={ref1}
- preload="metadata"
- src={colorMode === "dark" ? leftDark : leftLight}
- muted
- loop
- playsInline
- />
+ <AbsoluteFill>
+ <video
+ ref={ref1}
+ preload="metadata"
+ src={colorMode === "dark" ? leftDark : leftLight}
+ muted
+ loop
+ playsInline
+ />
+ </AbsoluteFill>
+ <AbsoluteFill>
+ <button
+ type="button"
+ style={{
+ width: 36,
+ height: 36,
+ bottom: 6,
+ left: 6,
+ position: "absolute",
+ appearance: "none",
+ border: "1px solid rgba(255, 255, 255, 0.1)",
+ outline: "1px solid rgba(0, 0, 0, 0.1)",
+ borderRadius: 4,
+ display: "flex",
+ justifyContent: "center",
+ alignItems: "center",
+ cursor: "pointer",
+ backgroundColor: "transparent",
+ pointerEvents: played ? "none" : "auto",
+ opacity: played ? 0 : 1,
+ transition: "opacity 0.2s",
+ }}
+ onClick={playAll}
+ >
+ <svg
+ style={{
+ height: 24,
+ }}
+ viewBox="0 0 384 512"
+ >
+ <path
+ fill="currentColor"
+ d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"
+ />
+ </svg>
+ </button>
+ </AbsoluteFill>
</div>
<Spacer />
<Spacer />