fix video

This commit is contained in:
2025-09-30 15:56:11 +02:00
parent ccce5ce264
commit 93994f27b2
3 changed files with 404 additions and 88 deletions
+175 -12
View File
@@ -10,7 +10,6 @@ import PlaybackItem from "./components/PlaybackItem.web";
const Playbacks = () => { const Playbacks = () => {
const { height: windowHeight } = useWindowDimensions(); const { height: windowHeight } = useWindowDimensions();
const [activeIndex, setActiveIndex] = useState(0); const [activeIndex, setActiveIndex] = useState(0);
const carouselRef = useRef(null);
const route = useRoute(); const route = useRoute();
const focusProjectId = const focusProjectId =
route?.params?.projectId || route?.params?.focusId || null; route?.params?.projectId || route?.params?.focusId || null;
@@ -25,15 +24,132 @@ const Playbacks = () => {
batchSize: 6, batchSize: 6,
}); });
const activePlayback = playbacks?.[activeIndex] || null;
const activeVideoUrl = activePlayback?.playbackUrl || null;
const backgroundVideoRef = useRef(null);
const lastActiveIndexRef = useRef(0);
const backgroundSeekPendingRef = useRef(false);
const listRef = useRef(null);
useEffect(() => {
const total = playbacks?.length || 0;
if (total <= 0) {
if (activeIndex !== 0) {
lastActiveIndexRef.current = 0;
setActiveIndex(0);
}
return;
}
const maxIndex = total - 1;
if (activeIndex > maxIndex) {
lastActiveIndexRef.current = maxIndex;
setActiveIndex(maxIndex);
}
}, [playbacks?.length, activeIndex]);
const handleActiveIndexChange = useCallback(
(rawIndex) => {
if (Number.isNaN(rawIndex)) return;
const total = playbacks?.length || 0;
const maxIndex = Math.max(0, total - 1);
const clamped = Math.max(0, Math.min(rawIndex, maxIndex));
if (clamped === lastActiveIndexRef.current) return;
lastActiveIndexRef.current = clamped;
setActiveIndex((prev) => (prev === clamped ? prev : clamped));
if (total > 0 && clamped >= Math.max(0, total - 6)) {
loadMore?.();
}
},
[playbacks?.length, loadMore]
);
const syncBackgroundVideo = useCallback(({ currentTime, isPlaying }) => {
const bg = backgroundVideoRef.current;
if (!bg) return;
if (typeof currentTime === "number" && Number.isFinite(currentTime)) {
const applyTime = () => {
backgroundSeekPendingRef.current = false;
const diff = Math.abs((bg.currentTime || 0) - currentTime);
if (diff > 0.25) {
try {
bg.currentTime = currentTime;
} catch (_e) {}
}
};
if (bg.readyState >= 1) applyTime();
else if (!backgroundSeekPendingRef.current) {
backgroundSeekPendingRef.current = true;
const handler = () => applyTime();
bg.addEventListener("loadeddata", handler, { once: true });
}
}
if (isPlaying === true) {
if (bg.paused) {
bg.play().catch(() => {});
}
} else if (isPlaying === false) {
try {
if (!bg.paused) bg.pause();
} catch (_e) {}
}
}, []);
useEffect(() => {
const bg = backgroundVideoRef.current;
if (!bg) return;
if (!activeVideoUrl) {
backgroundSeekPendingRef.current = false;
try {
bg.pause();
} catch (_e) {}
bg.removeAttribute?.("src");
bg.load?.();
return;
}
backgroundSeekPendingRef.current = false;
const setSrcIfNeeded = () => {
const attrSrc = bg.getAttribute("src");
if (attrSrc !== activeVideoUrl) {
bg.setAttribute("src", activeVideoUrl);
try {
bg.load();
} catch (_e) {}
}
};
const startPlayback = () => {
bg.play().catch(() => {});
};
setSrcIfNeeded();
if (bg.readyState >= 2) {
startPlayback();
return;
}
const handleLoaded = () => {
startPlayback();
};
bg.addEventListener("loadeddata", handleLoaded, { once: true });
return () => {
bg.removeEventListener("loadeddata", handleLoaded);
};
}, [activeVideoUrl]);
const triedLoadMoreRef = useRef(0); const triedLoadMoreRef = useRef(0);
useEffect(() => { useEffect(() => {
if (!focusProjectId) return; if (!focusProjectId) return;
const idx = playbacks.findIndex((p) => p?.id === focusProjectId); const idx = playbacks.findIndex((p) => p?.id === focusProjectId);
if (idx >= 0) { if (idx >= 0) {
lastActiveIndexRef.current = idx;
setActiveIndex(idx); setActiveIndex(idx);
setTimeout(() => { setTimeout(() => {
try { try {
carouselRef.current?.scrollTo?.({ index: idx, animated: false }); listRef.current?.scrollToIndex?.({ index: idx, animated: false });
} catch (_e) {} } catch (_e) {}
}, 50); }, 50);
} else if (loadMore && triedLoadMoreRef.current < 6) { } else if (loadMore && triedLoadMoreRef.current < 6) {
@@ -43,22 +159,27 @@ const Playbacks = () => {
}, [focusProjectId, playbacks, loadMore]); }, [focusProjectId, playbacks, loadMore]);
// === FlatList (one real page per item) === // === FlatList (one real page per item) ===
const listRef = useRef(null);
// keep active index in sync with scroll // keep active index in sync with scroll
const onMomentumScrollEnd = useCallback( const onMomentumScrollEnd = useCallback(
(e) => { (e) => {
try { try {
const y = e?.nativeEvent?.contentOffset?.y || 0; const y = e?.nativeEvent?.contentOffset?.y || 0;
const idx = Math.round(y / windowHeight); const idx = Math.round(y / windowHeight);
if (!Number.isNaN(idx)) handleActiveIndexChange(idx);
setActiveIndex(
Math.max(0, Math.min(idx, (playbacks?.length || 1) - 1))
);
if (idx >= (playbacks?.length || 0) - 6) loadMore?.();
} catch (_e) {} } catch (_e) {}
}, },
[windowHeight, playbacks?.length, loadMore] [windowHeight, handleActiveIndexChange]
);
const onScroll = useCallback(
(e) => {
try {
const y = e?.nativeEvent?.contentOffset?.y || 0;
const idx = Math.round(y / windowHeight);
handleActiveIndexChange(idx);
} catch (_e) {}
},
[windowHeight, handleActiveIndexChange]
); );
// programmatic jump to focus item // programmatic jump to focus item
@@ -66,6 +187,7 @@ const Playbacks = () => {
if (!focusProjectId) return; if (!focusProjectId) return;
const idx = playbacks.findIndex((p) => p?.id === focusProjectId); const idx = playbacks.findIndex((p) => p?.id === focusProjectId);
if (idx >= 0) { if (idx >= 0) {
lastActiveIndexRef.current = idx;
setActiveIndex(idx); setActiveIndex(idx);
setTimeout(() => { setTimeout(() => {
try { try {
@@ -85,8 +207,36 @@ const Playbacks = () => {
); );
return ( return (
<Page headerType="NONE" width="100%" containerStyle={{ padding: 0 }}> <Page
headerType="NONE"
width="100%"
containerStyle={{ padding: 0 }}
>
<View style={styles.screen}> <View style={styles.screen}>
{activeVideoUrl ? (
<View pointerEvents="none" style={styles.backgroundVideoWrapper}>
<video
ref={backgroundVideoRef}
key={activeVideoUrl}
src={activeVideoUrl}
autoPlay
muted
loop
playsInline
preload="auto"
style={{
position: "absolute",
inset: 0,
width: "100%",
height: "100%",
objectFit: "cover",
filter: "blur(28px) saturate(120%) brightness(55%)",
transform: "scale(1.1)",
}}
/>
<View style={styles.backgroundOverlay} />
</View>
) : null}
<FlatList <FlatList
ref={listRef} ref={listRef}
data={playbacks} data={playbacks}
@@ -98,6 +248,7 @@ const Playbacks = () => {
userCache={userCache} userCache={userCache}
getUserByUid={getUserByUid} getUserByUid={getUserByUid}
isActive={isFocused && index === activeIndex} isActive={isFocused && index === activeIndex}
onBackgroundSync={syncBackgroundVideo}
/> />
</View> </View>
)} )}
@@ -108,7 +259,8 @@ const Playbacks = () => {
initialNumToRender={3} initialNumToRender={3}
windowSize={5} windowSize={5}
removeClippedSubviews removeClippedSubviews
scrollEventThrottle={32} onScroll={onScroll}
scrollEventThrottle={16}
/> />
</View> </View>
</Page> </Page>
@@ -121,5 +273,16 @@ const styles = StyleSheet.create({
screen: { screen: {
flex: 1, flex: 1,
overscrollBehavior: "none", overscrollBehavior: "none",
position: "relative",
},
backgroundVideoWrapper: {
...StyleSheet.absoluteFillObject,
overflow: "hidden",
zIndex: -1,
backgroundColor: "#060606",
},
backgroundOverlay: {
...StyleSheet.absoluteFillObject,
backgroundColor: "rgba(2, 2, 2, 0.35)",
}, },
}); });
@@ -32,12 +32,21 @@ import { FONT_FAMILY } from "../../../styles/Fonts";
import { size } from "../../../styles/Style"; import { size } from "../../../styles/Style";
import CommentsPanel from "./CommentsPanel.web"; import CommentsPanel from "./CommentsPanel.web";
// Debug logging toggle for web playback
const DEBUG_PLAYBACK_WEB = true;
// NOTE: Web-only implementation that uses a native <video> element instead of expo-video // NOTE: Web-only implementation that uses a native <video> element instead of expo-video
// - No Platform checks // - No Platform checks
// - Handles autoplay requirements (muted first, unmute on first gesture) // - Always relies on an external audio track for playback
// - Keeps external audio track in sync when present // - Keeps the muted video visuals in sync with the external audio clock
const PlaybackItem = ({ item, isActive, userCache, getUserByUid }) => { const PlaybackItem = ({
item,
isActive,
userCache,
getUserByUid,
onBackgroundSync,
}) => {
const { width: viewportWidth, height: viewportHeight } = const { width: viewportWidth, height: viewportHeight } =
useWindowDimensions(); useWindowDimensions();
@@ -51,13 +60,15 @@ const PlaybackItem = ({ item, isActive, userCache, getUserByUid }) => {
const videoUrl = item?.playbackUrl || null; const videoUrl = item?.playbackUrl || null;
const videoRef = useRef(null); // HTMLVideoElement const videoRef = useRef(null); // HTMLVideoElement
const wasActiveRef = useRef(false);
const startedRef = useRef(false);
const debugTickRef = useRef(0);
const pendingSeekRef = useRef(false);
const audioSource = useMemo(() => { const audioSource = useMemo(() => {
const fromSong = item?.songUrl ? { uri: item.songUrl } : null; const fromSong = item?.songUrl ? { uri: item.songUrl } : null;
return fromSong; return fromSong;
}, [item]); }, [item?.songUrl]);
const hasExternalAudio = !!audioSource;
const initialLikedBy = Array.isArray(item?.likedBy) ? item.likedBy : []; const initialLikedBy = Array.isArray(item?.likedBy) ? item.likedBy : [];
const [isLiked, setIsLiked] = useState( const [isLiked, setIsLiked] = useState(
@@ -135,6 +146,30 @@ const PlaybackItem = ({ item, isActive, userCache, getUserByUid }) => {
const audioPlayer = useAudioPlayer(audioSource || undefined); const audioPlayer = useAudioPlayer(audioSource || undefined);
// Helpers to normalize and map clocks
const getAudioTimeSeconds = useCallback(() => {
const raw = Number(audioPlayer?.currentTime || 0);
// On web, expo-audio currentTime can be in ms; convert if it looks like ms (>1000)
const seconds = raw > 1000 ? raw / 1000 : raw;
return Number.isFinite(seconds) ? seconds : 0;
}, [audioPlayer]);
const getTargetVideoTime = useCallback(
(el) => {
const sec = getAudioTimeSeconds();
const dur = Number(el?.duration || NaN);
if (Number.isFinite(dur) && dur > 0.3) {
let t = sec % dur;
// Avoid setting time extremely close to the end to prevent immediate loop
const guard = Math.max(0, dur - 0.25);
if (t > guard) t = guard;
return t;
}
return sec;
},
[getAudioTimeSeconds]
);
// --- Playback control helpers (web <video>) --- // --- Playback control helpers (web <video>) ---
const playVideo = useCallback(async () => { const playVideo = useCallback(async () => {
try { try {
@@ -148,96 +183,133 @@ const PlaybackItem = ({ item, isActive, userCache, getUserByUid }) => {
} catch (_e) {} } catch (_e) {}
}, []); }, []);
const unmuteVideoOnFirstGesture = useCallback(() => { const syncVideoClockToAudio = useCallback(() => {
if (typeof document === "undefined") return () => {};
const handler = async () => {
try { try {
const el = videoRef.current; const el = videoRef.current;
if (!el) return; if (!el) return;
if (!hasExternalAudio) { const audioTimeS = getAudioTimeSeconds();
el.muted = false; // restore sound if video is the only audio const audioIsPlaying = !!audioPlayer?.playing || audioTimeS > 0.05;
if (!audioIsPlaying) return;
const target = getTargetVideoTime(el);
// If not enough data loaded to seek accurately, attach one-time seek on loadeddata
if (el.readyState < 2) {
if (!pendingSeekRef.current) {
pendingSeekRef.current = true;
const handler = () => {
try {
const t = getTargetVideoTime(el);
el.currentTime = t;
if (el.paused) el.play().catch(() => {});
if (DEBUG_PLAYBACK_WEB) {
console.log("[PlaybackItem.web] deferred seek on loadeddata", {
t,
rs: el.readyState,
});
} }
if (el.paused) await el.play(); } catch (_) {}
if (hasExternalAudio && audioPlayer && !audioPlayer.playing) { pendingSeekRef.current = false;
await audioPlayer.play?.(); };
el.addEventListener("loadeddata", handler, { once: true });
}
return;
}
const diff = Math.abs((el.currentTime || 0) - target);
if (diff > 0.5) {
if (DEBUG_PLAYBACK_WEB) {
console.log("[PlaybackItem.web] sync video", {
audioTimeS,
target,
videoTime: Number(el.currentTime || 0),
duration: Number(el.duration || 0),
});
}
el.currentTime = target;
if (el.paused) el.play().catch(() => {});
} }
} catch (_e) {} } catch (_e) {}
}; }, [audioPlayer, getAudioTimeSeconds, getTargetVideoTime]);
const events = [
"pointerdown",
"click",
"touchstart",
"keydown",
"wheel",
"scroll",
];
events.forEach((ev) =>
document.addEventListener(ev, handler, { once: true, passive: true })
);
return () => {
events.forEach((ev) => document.removeEventListener(ev, handler));
};
}, [audioPlayer, hasExternalAudio]);
// Start/reset when active // Start/reset when active
useEffect(() => { useEffect(() => {
const el = videoRef.current; const el = videoRef.current;
if (!isActive || !el) return; if (!isActive || !el) return;
if (startedRef.current) return;
const start = async () => { const start = async () => {
try { try {
// Reset clocks if (DEBUG_PLAYBACK_WEB) {
if (audioPlayer && hasExternalAudio) await audioPlayer.seekTo?.(0); console.log("[PlaybackItem.web] start()", {
el.currentTime = 0; isActive,
audioPlaying: !!audioPlayer?.playing,
await playVideo(); audioTime: Number(audioPlayer?.currentTime || 0),
if (hasExternalAudio && audioPlayer && !audioPlayer.playing) { videoPaused: el.paused,
await audioPlayer.play?.(); videoTime: Number(el.currentTime || 0),
readyState: el.readyState,
});
} }
// 1) Start the visual first to guarantee motion
await playVideo();
// 2) Try to start audio, but don't force sync until it's actually playing
if (audioPlayer && !audioPlayer.playing) {
try {
await audioPlayer.play?.();
} catch (_e) {}
}
// 3) Choose the best clock available for initial background sync
const audioTimeS = getAudioTimeSeconds();
const audioIsPlaying = !!audioPlayer?.playing || audioTimeS > 0.05;
const videoTime = Number(el.currentTime || 0);
const clockTime = audioIsPlaying ? getTargetVideoTime(el) : videoTime;
onBackgroundSync?.({
currentTime: Number.isFinite(clockTime) ? clockTime : 0,
isPlaying: audioIsPlaying || !el.paused,
});
startedRef.current = true;
} catch (_e) {} } catch (_e) {}
}; };
start(); start();
const removeGesture = unmuteVideoOnFirstGesture(); return () => {};
return () => {
removeGesture?.();
};
}, [ }, [
isActive, isActive,
playVideo, playVideo,
audioPlayer, audioPlayer,
hasExternalAudio, syncVideoClockToAudio,
unmuteVideoOnFirstGesture, onBackgroundSync,
]); ]);
// Keep external audio & video roughly in sync (when audio is the source of truth)
useEffect(() => { useEffect(() => {
if (!isActive || !hasExternalAudio) return; if (isActive) return;
const id = setInterval(() => {
try { try {
if (!audioPlayer?.playing) return; if (audioPlayer?.playing) audioPlayer.pause?.();
} catch (_e) {}
const el = videoRef.current; const el = videoRef.current;
if (!el) return; if (!el) return;
const a = Number(audioPlayer?.currentTime || 0); try {
const v = Number(el.currentTime || 0); if (!el.paused) el.pause();
if (Math.abs(a - v) > 0.2) { el.muted = true;
el.currentTime = Math.max(0, a);
}
} catch (_e) {} } catch (_e) {}
}, 300); startedRef.current = false;
return () => clearInterval(id); }, [isActive, audioPlayer]);
}, [isActive, hasExternalAudio, audioPlayer]);
// Pause all on unmount // Pause all on unmount
useEffect(() => { useEffect(() => {
const el = videoRef.current; // snapshot ref for cleanup
return () => { return () => {
try { try {
const el = videoRef.current;
if (audioPlayer?.playing) audioPlayer.pause?.(); if (audioPlayer?.playing) audioPlayer.pause?.();
if (el && !el.paused) el.pause(); if (el && !el.paused) el.pause();
} catch (_e) {} } catch (_e) {}
onBackgroundSync?.({ isPlaying: false });
}; };
}, [audioPlayer]); }, [audioPlayer, onBackgroundSync]);
// Lyrics timing // Lyrics timing
const alignedWords = useMemo(() => { const alignedWords = useMemo(() => {
@@ -256,15 +328,67 @@ const PlaybackItem = ({ item, isActive, userCache, getUserByUid }) => {
if (!isActive) return; if (!isActive) return;
const id = setInterval(() => { const id = setInterval(() => {
try { try {
const useAudioTime = hasExternalAudio && !!audioPlayer?.playing; const el = videoRef.current;
const t = useAudioTime const audioTimeS = getAudioTimeSeconds();
? Number(audioPlayer?.currentTime || 0) const audioIsPlaying = !!audioPlayer?.playing || audioTimeS > 0.05;
: Number(videoRef.current?.currentTime || 0); const videoTime = Number(el?.currentTime || 0);
setCurrentTimeS(t);
// Lyrics: real audio seconds (normalized), not modulo
const timeForLyrics =
audioIsPlaying && Number.isFinite(audioTimeS)
? audioTimeS
: Number.isFinite(videoTime)
? videoTime
: 0;
setCurrentTimeS(timeForLyrics);
// Keep video roughly in sync with audio only when audio is genuinely playing
if (audioIsPlaying) {
syncVideoClockToAudio();
}
onBackgroundSync?.({
// Background video: use the same modulo/clamped target as the main video to avoid loop-thrashing
currentTime: getTargetVideoTime(el),
isPlaying: audioIsPlaying || (!!el && !el.paused),
});
if (DEBUG_PLAYBACK_WEB) {
const n = (debugTickRef.current = (debugTickRef.current + 1) % 4);
if (n === 0) {
console.log("[PlaybackItem.web] tick", {
audioPlaying: !!audioPlayer?.playing,
audioTimeS,
videoPaused: !!el?.paused,
videoTime,
started: startedRef.current,
});
}
}
} catch (_e) {} } catch (_e) {}
}, 250); }, 250);
return () => clearInterval(id); return () => clearInterval(id);
}, [isActive, hasExternalAudio, audioPlayer]); }, [
isActive,
audioPlayer,
syncVideoClockToAudio,
onBackgroundSync,
getAudioTimeSeconds,
getTargetVideoTime,
]);
useEffect(() => {
if (!onBackgroundSync) return undefined;
if (isActive) {
wasActiveRef.current = true;
return undefined;
}
if (wasActiveRef.current) {
onBackgroundSync({ isPlaying: false });
wasActiveRef.current = false;
}
return undefined;
}, [isActive, onBackgroundSync]);
const descriptionText = const descriptionText =
item?.description || item?.title || "Description chanson"; item?.description || item?.title || "Description chanson";
@@ -304,6 +428,37 @@ const PlaybackItem = ({ item, isActive, userCache, getUserByUid }) => {
); );
const panelHeight = Math.min(videoHeight, desiredHeight); const panelHeight = Math.min(videoHeight, desiredHeight);
// Attach verbose event listeners on the HTML video element
useEffect(() => {
const el = videoRef.current;
if (!el || !DEBUG_PLAYBACK_WEB) return undefined;
const handler = (e) => {
// Avoid heavy logs: only show key events and brief state
console.log("[PlaybackItem.web] video:", e.type, {
t: Number(el.currentTime || 0).toFixed(2),
paused: el.paused,
rs: el.readyState,
});
};
const events = [
"loadedmetadata",
"loadeddata",
"play",
"playing",
"pause",
"seeking",
"seeked",
"stalled",
"waiting",
"ended",
"error",
];
events.forEach((ev) => el.addEventListener(ev, handler));
return () => {
events.forEach((ev) => el.removeEventListener(ev, handler));
};
}, []);
return ( return (
<View <View
onLayout={handleLayout} onLayout={handleLayout}
@@ -332,8 +487,6 @@ const PlaybackItem = ({ item, isActive, userCache, getUserByUid }) => {
src={videoUrl} src={videoUrl}
playsInline playsInline
muted muted
autoPlay
loop
preload="auto" preload="auto"
// Using web CSS properties here on purpose // Using web CSS properties here on purpose
style={{ style={{
@@ -1,10 +1,10 @@
import { View, Text, Pressable, Platform } from "react-native";
import React, { useState } from "react";
import { BlurView } from "expo-blur"; import { BlurView } from "expo-blur";
import React, { useState } from "react";
import { Pressable, Text, View } from "react-native";
import BorderGradient from "../../../components/BorderGradient/BorderGradient"; import BorderGradient from "../../../components/BorderGradient/BorderGradient";
import useLayoutType from "../../../hooks/useLayoutType";
import { Palette } from "../../../styles"; import { Palette } from "../../../styles";
import { FONT_FAMILY } from "../../../styles/Fonts"; import { FONT_FAMILY } from "../../../styles/Fonts";
import useLayoutType from "../../../hooks/useLayoutType";
const CreateLyricsHeader = ({ const CreateLyricsHeader = ({
title = "", title = "",