cosmo/frontend/src/components/InterstellarTicker.tsx

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-11-30 02:43:47 +00:00
import { useEffect, useRef } from 'react';
2025-11-29 16:58:58 +00:00
interface InterstellarTickerProps {
isPlaying: boolean;
}
export function InterstellarTicker({ isPlaying }: InterstellarTickerProps) {
2025-11-30 02:43:47 +00:00
const audioRef = useRef<HTMLAudioElement | null>(null);
2025-11-29 16:58:58 +00:00
2025-11-30 02:43:47 +00:00
useEffect(() => {
if (!audioRef.current) {
// Initialize audio element
const protocol = window.location.protocol;
const hostname = window.location.hostname;
const port = import.meta.env.VITE_API_BASE_URL ? '' : ':8000';
const audioUrl = `${protocol}//${hostname}${port}/upload/assets/tick_sample.m4a`;
const audio = new Audio(audioUrl);
audio.loop = true; // Enable looping
audio.volume = 0.5; // Set default volume
audioRef.current = audio;
2025-11-29 16:58:58 +00:00
}
if (isPlaying) {
2025-11-30 02:43:47 +00:00
audioRef.current.play().catch(e => console.error("Audio play failed:", e));
2025-11-29 16:58:58 +00:00
} else {
2025-11-30 02:43:47 +00:00
audioRef.current.pause();
// Optional: reset time to 0 if you want it to start from beginning each time
// audioRef.current.currentTime = 0;
2025-11-29 16:58:58 +00:00
}
return () => {
2025-11-30 02:43:47 +00:00
// Cleanup not strictly necessary for singleton audio, but good practice if component unmounts
if (audioRef.current) {
audioRef.current.pause();
2025-11-29 16:58:58 +00:00
}
};
2025-11-30 02:43:47 +00:00
}, [isPlaying]);
2025-11-29 16:58:58 +00:00
return null;
}