Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: autoplay functionality for embed

Richie McIlroyab95bb5e8ce6
diff --git a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx
index a8141c7ab..c45792e2b 100644
--- a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx
+++ b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx
@@ -67,10 +67,19 @@ export const EmbedVideo = forwardRef<
chapters?: { title: string; start: number }[];
aiProcessing?: boolean;
ownerName?: string | null;
+ autoplay?: boolean;
}
>(
(
- { data, user, comments, chapters = [], aiProcessing = false, ownerName },
+ {
+ data,
+ user,
+ comments,
+ chapters = [],
+ aiProcessing = false,
+ ownerName,
+ autoplay = false,
+ },
ref
) => {
useImperativeHandle(ref, () => videoRef.current as HTMLVideoElement);
@@ -296,10 +305,23 @@ export const EmbedVideo = forwardRef<
videoElement.addEventListener("timeupdate", handleTimeUpdate);
+ if (autoplay && !isPlaying) {
+ const attemptAutoplay = async () => {
+ try {
+ videoElement.muted = true;
+ await videoElement.play();
+ setIsPlaying(true);
+ } catch (error) {
+ console.log("Autoplay failed:", error);
+ }
+ };
+ attemptAutoplay();
+ }
+
return () => {
videoElement.removeEventListener("timeupdate", handleTimeUpdate);
};
- }, [videoReadyToPlay]);
+ }, [videoReadyToPlay, autoplay, isPlaying]);
useEffect(() => {
setTempOverlayVisible(true);
diff --git a/apps/web/app/embed/[videoId]/page.tsx b/apps/web/app/embed/[videoId]/page.tsx
index 48e553beb..c5671f94c 100644
--- a/apps/web/app/embed/[videoId]/page.tsx
+++ b/apps/web/app/embed/[videoId]/page.tsx
@@ -24,6 +24,7 @@ export const revalidate = 30;
type Props = {
params: { [key: string]: string | string[] | undefined };
+ searchParams: { [key: string]: string | string[] | undefined };
};
export async function generateMetadata({ params }: Props): Promise<Metadata> {
@@ -111,7 +112,9 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
export default async function EmbedVideoPage(props: Props) {
const params = props.params;
+ const searchParams = props.searchParams;
const videoId = params.videoId as string;
+ const autoplay = searchParams.autoplay === "true";
const user = await getCurrentUser();
@@ -169,7 +172,7 @@ export default async function EmbedVideoPage(props: Props) {
videoId={video.id}
/>
{userAccess === "has-access" && (
- <EmbedContent video={video} user={user} />
+ <EmbedContent video={video} user={user} autoplay={autoplay} />
)}
</div>
);
@@ -178,11 +181,13 @@ export default async function EmbedVideoPage(props: Props) {
async function EmbedContent({
video,
user,
+ autoplay,
}: {
video: typeof videos.$inferSelect & {
sharedOrganization: { organizationId: string } | null;
};
user: typeof users.$inferSelect | null;
+ autoplay: boolean;
}) {
let aiGenerationEnabled = false;
const videoOwnerQuery = await db()
@@ -298,6 +303,7 @@ async function EmbedContent({
chapters={initialAiData?.chapters || []}
aiProcessing={initialAiData?.processing || false}
ownerName={videoOwner[0]?.name || null}
+ autoplay={autoplay}
/>
);
}