'use client'; import { useState, useRef, useEffect } from 'react'; import { useAudio } from './AudioProvider'; interface AudioPlayerProps { fileName: string; datasetId: number; externalId?: string; } export default function AudioPlayer({ fileName, datasetId, externalId }: AudioPlayerProps) { const [isPlaying, setIsPlaying] = useState(false); const audioRef = useRef(null); const { currentAudioId, setCurrentAudio } = useAudio(); const audioId = `${datasetId}-${externalId}`; // Extract file extension from original fileName and construct URL using externalId const fileExtension = fileName.substring(fileName.lastIndexOf('.')); const audioPath = `/public/datasets/${datasetId}/${externalId}${fileExtension}`; // Stop playing if another audio started useEffect(() => { if (currentAudioId !== audioId && audioRef.current && isPlaying) { audioRef.current.pause(); audioRef.current.currentTime = 0; setIsPlaying(false); } }, [currentAudioId, audioId, isPlaying]); const handlePlay = () => { if (audioRef.current) { setCurrentAudio(audioId); audioRef.current.currentTime = 0; audioRef.current.play(); setIsPlaying(true); } }; const handleStop = () => { if (audioRef.current) { audioRef.current.pause(); audioRef.current.currentTime = 0; setCurrentAudio(null); setIsPlaying(false); } }; const handlePlaybackEnd = () => { setIsPlaying(false); setCurrentAudio(null); }; return (
); }