Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: Video/audio sync in player

Richie McIlroya06ce0dd3c9a
diff --git a/apps/web/app/share/[videoId]/_components/AudioPlayer.tsx b/apps/web/app/share/[videoId]/_components/AudioPlayer.tsx
index 6dcb9f129..52042cccf 100644
--- a/apps/web/app/share/[videoId]/_components/AudioPlayer.tsx
+++ b/apps/web/app/share/[videoId]/_components/AudioPlayer.tsx
@@ -1,57 +1,32 @@
-import React, { useEffect, useRef } from "react";
+import React, { forwardRef, useEffect } from "react";
import Hls from "hls.js";
interface AudioPlayerProps {
src: string;
- isPlaying: boolean;
- currentTime: number;
}
-export const AudioPlayer: React.FC<AudioPlayerProps> = ({
- src,
- isPlaying,
- currentTime,
-}) => {
- const audioRef = useRef<HTMLAudioElement | null>(null);
-
- useEffect(() => {
- const audio = audioRef.current;
- if (!audio) return;
-
- const hls = new Hls({
- autoStartLoad: true,
- enableWorker: true,
- debug: true,
- });
- if (Hls.isSupported()) {
- hls.loadSource(src);
- hls.attachMedia(audio);
- } else if (audio.canPlayType("application/vnd.apple.mpegurl")) {
- audio.src = src;
- }
-
- return () => {
- hls.destroy();
- };
- }, [src]);
-
- useEffect(() => {
- const audio = audioRef.current;
- if (!audio) return;
-
- if (isPlaying) {
- audio.play().catch((e) => console.error("Audio playback failed:", e));
- } else {
- audio.pause();
- }
- }, [isPlaying]);
-
- useEffect(() => {
- const audio = audioRef.current;
- if (!audio || !audio.duration) return;
-
- audio.currentTime = currentTime;
- }, [currentTime]);
-
- return <audio ref={audioRef} controls={false} style={{ display: "none" }} />;
-};
+export const AudioPlayer = forwardRef<HTMLAudioElement, AudioPlayerProps>(
+ ({ src }, ref) => {
+ useEffect(() => {
+ if (!ref || typeof ref === "function") return;
+ const audio = ref.current;
+
+ if (Hls.isSupported()) {
+ const hls = new Hls();
+ hls.loadSource(src);
+ if (audio) {
+ hls.attachMedia(audio);
+ }
+ return () => {
+ if (audio) {
+ hls.destroy();
+ }
+ };
+ } else if (audio && audio.canPlayType("application/vnd.apple.mpegurl")) {
+ audio.src = src;
+ }
+ }, [src, ref]);
+
+ return <audio ref={ref} controls={false} style={{ display: "none" }} />;
+ }
+);
diff --git a/apps/web/app/share/[videoId]/_components/ShareVideo.tsx b/apps/web/app/share/[videoId]/_components/ShareVideo.tsx
index 74947aa01..e6f30ff37 100644
--- a/apps/web/app/share/[videoId]/_components/ShareVideo.tsx
+++ b/apps/web/app/share/[videoId]/_components/ShareVideo.tsx
@@ -16,6 +16,7 @@ const formatTime = (time: number) => {
export const ShareVideo = ({ data }: { data: typeof videos.$inferSelect }) => {
const video1Ref = useRef<HTMLVideoElement>(null);
const video2Ref = useRef<HTMLVideoElement>(null);
+ const audioPlayerRef = useRef<HTMLAudioElement>(null);
const [isPlaying, setIsPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
const [isLoading, setIsLoading] = useState(true);
@@ -65,7 +66,18 @@ export const ShareVideo = ({ data }: { data: typeof videos.$inferSelect }) => {
};
}, [seeking]);
- const handlePlayPauseClick = () => setIsPlaying(!isPlaying);
+ const handlePlayPauseClick = () => {
+ setIsPlaying(!isPlaying);
+ if (!isPlaying) {
+ video1Ref.current?.play();
+ video2Ref.current?.play();
+ audioPlayerRef.current?.play();
+ } else {
+ video1Ref.current?.pause();
+ video2Ref.current?.pause();
+ audioPlayerRef.current?.pause();
+ }
+ };
const handleMuteClick = () => {
const muted = !video1Ref.current?.muted;
@@ -158,10 +170,43 @@ export const ShareVideo = ({ data }: { data: typeof videos.$inferSelect }) => {
}
}, [isPlaying, isLoading]);
- const onAudioLoaded = (duration: number) => {
- const videoDuration = video1Ref.current?.duration || 0;
- setLongestDuration(Math.max(duration, videoDuration));
- };
+ useEffect(() => {
+ const video = video1Ref.current;
+ const audio = audioPlayerRef.current;
+
+ // Function to synchronize audio playback with video
+ const synchronizePlayback = () => {
+ if (!video || !audio) return;
+
+ // Calculate the time drift between audio and video
+ const drift = Math.abs(video.currentTime - audio.currentTime);
+
+ // Adjust audio currentTime if drift exceeds a small tolerance (e.g., 0.1 seconds)
+ if (drift > 0.1) {
+ audio.currentTime = video.currentTime;
+ }
+ };
+
+ // Sync audio on video seek
+ const handleSeek = () => {
+ synchronizePlayback(); // Adjust audio to match video currentTime
+ if (isPlaying) {
+ audio?.play().catch((e) => console.error("Audio playback failed:", e)); // Ensure audio resumes if it was playing
+ }
+ };
+
+ // Add event listeners
+ video?.addEventListener("seeked", handleSeek);
+ video?.addEventListener("timeupdate", synchronizePlayback);
+
+ // Cleanup event listeners
+ return () => {
+ if (video) {
+ video.removeEventListener("seeked", handleSeek);
+ video.removeEventListener("timeupdate", synchronizePlayback);
+ }
+ };
+ }, [isPlaying]); // Dependency on isPlaying ensures updates when playback state changes
return (
<div
@@ -175,7 +220,7 @@ export const ShareVideo = ({ data }: { data: typeof videos.$inferSelect }) => {
)}
{isLoading === false && (
<div
- className={`absolute top-0 left-0 w-full h-full z-10 flex items-center justify-center bg-black bg-opacity-50 transition-all opacity-0 group-hover:opacity-100`}
+ className={`absolute top-0 left-0 w-full h-full z-10 flex items-center justify-center bg-black bg-opacity-50 transition-all opacity-0 group-hover:opacity-100 z-20`}
>
<button
aria-label="Play video"
@@ -193,11 +238,10 @@ export const ShareVideo = ({ data }: { data: typeof videos.$inferSelect }) => {
</div>
)}
<AudioPlayer
+ ref={audioPlayerRef}
src={`${process.env.NEXT_PUBLIC_URL}/api/playlist?userId=${data.ownerId}&videoId=${data.id}&videoType=audio`}
- isPlaying={isPlaying}
- currentTime={currentTime}
/>
- <div className="w-[175px] h-[175px] absolute bottom-4 right-12 overflow-hidden rounded-full z-10 shadow-lg">
+ <div className="w-[200px] h-[200px] absolute bottom-4 right-12 overflow-hidden rounded-full z-10 shadow-lg">
<VideoPlayer
ref={video1Ref}
src={`${process.env.NEXT_PUBLIC_URL}/api/playlist?userId=${data.ownerId}&videoId=${data.id}&videoType=video`}