reverted audio effects changes - can not bypass browser restrictions
This commit is contained in:
parent
33a4abe45f
commit
a5a936fe73
@ -83,10 +83,6 @@ export function useAudioEffects({
|
|||||||
const wasHoveredRef = useRef(false);
|
const wasHoveredRef = useRef(false);
|
||||||
const wasActiveRef = useRef(false);
|
const wasActiveRef = useRef(false);
|
||||||
|
|
||||||
// Delay audio playback after mount/reset to avoid browser autoplay policy
|
|
||||||
// User can't hover during mount, so any true state within 100ms is stale
|
|
||||||
const [canPlay, setCanPlay] = useState(false);
|
|
||||||
|
|
||||||
// Track which URLs we've already fetched to prevent duplicate fetches
|
// Track which URLs we've already fetched to prevent duplicate fetches
|
||||||
const lastFetchedHoverUrlRef = useRef<string | null>(null);
|
const lastFetchedHoverUrlRef = useRef<string | null>(null);
|
||||||
const lastFetchedClickUrlRef = useRef<string | null>(null);
|
const lastFetchedClickUrlRef = useRef<string | null>(null);
|
||||||
@ -99,18 +95,6 @@ export function useAudioEffects({
|
|||||||
const [hoverAudioReady, setHoverAudioReady] = useState(false);
|
const [hoverAudioReady, setHoverAudioReady] = useState(false);
|
||||||
const [clickAudioReady, setClickAudioReady] = useState(false);
|
const [clickAudioReady, setClickAudioReady] = useState(false);
|
||||||
|
|
||||||
// Enable audio playback after short delay to skip stale state at mount
|
|
||||||
// Any hover/active state within 100ms of mount is not from real user interaction
|
|
||||||
useEffect(() => {
|
|
||||||
setCanPlay(false);
|
|
||||||
wasHoveredRef.current = isHovered;
|
|
||||||
wasActiveRef.current = isActive;
|
|
||||||
const timer = setTimeout(() => setCanPlay(true), 100);
|
|
||||||
return () => clearTimeout(timer);
|
|
||||||
// Only reset on resetKey change, not on isHovered/isActive changes
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [resetKey]);
|
|
||||||
|
|
||||||
// Initialize hover audio element - fetch with credentials for auth
|
// Initialize hover audio element - fetch with credentials for auth
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!hoverAudioUrl) {
|
if (!hoverAudioUrl) {
|
||||||
@ -254,9 +238,7 @@ export function useAudioEffects({
|
|||||||
|
|
||||||
// Play hover audio when hover starts (plays to completion, not interrupted)
|
// Play hover audio when hover starts (plays to completion, not interrupted)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Only play if: canPlay is true, hover changed from false→true, audio is ready
|
|
||||||
if (
|
if (
|
||||||
canPlay &&
|
|
||||||
isHovered &&
|
isHovered &&
|
||||||
!wasHoveredRef.current &&
|
!wasHoveredRef.current &&
|
||||||
hoverAudioRef.current &&
|
hoverAudioRef.current &&
|
||||||
@ -271,13 +253,11 @@ export function useAudioEffects({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
wasHoveredRef.current = isHovered;
|
wasHoveredRef.current = isHovered;
|
||||||
}, [canPlay, isHovered, hoverAudioReady]);
|
}, [isHovered, hoverAudioReady]);
|
||||||
|
|
||||||
// Play click audio when active state begins
|
// Play click audio when active state begins
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Only play if: canPlay is true, active changed from false→true, audio is ready
|
|
||||||
if (
|
if (
|
||||||
canPlay &&
|
|
||||||
isActive &&
|
isActive &&
|
||||||
!wasActiveRef.current &&
|
!wasActiveRef.current &&
|
||||||
clickAudioRef.current &&
|
clickAudioRef.current &&
|
||||||
@ -290,7 +270,7 @@ export function useAudioEffects({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
wasActiveRef.current = isActive;
|
wasActiveRef.current = isActive;
|
||||||
}, [canPlay, isActive, clickAudioReady]);
|
}, [isActive, clickAudioReady]);
|
||||||
|
|
||||||
// Manual click audio trigger
|
// Manual click audio trigger
|
||||||
const playClickAudio = useCallback(() => {
|
const playClickAudio = useCallback(() => {
|
||||||
@ -313,10 +293,11 @@ export function useAudioEffects({
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Stop audio on key change (e.g., page navigation or element change)
|
// Reset audio on key change (e.g., page navigation)
|
||||||
// Note: canPlay and refs are reset in the canPlay effect above
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
stopAll();
|
stopAll();
|
||||||
|
wasHoveredRef.current = false;
|
||||||
|
wasActiveRef.current = false;
|
||||||
}, [resetKey, stopAll]);
|
}, [resetKey, stopAll]);
|
||||||
|
|
||||||
return { playClickAudio, stopAll };
|
return { playClickAudio, stopAll };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user