import React, { useState, useEffect, useRef } from 'react'; import { FaMicrophone, FaPlay, FaPause, FaVolumeUp, FaTrash } from 'react-icons/fa'; import { BiPodcast } from 'react-icons/bi'; import { MdDateRange } from 'react-icons/md'; import DeleteModal from '../components/DeleteModal'; import Toast from '../components/Toast'; import './Podcasts.css'; const AudioPlayer = ({ audioUrl }) => { const [isPlaying, setIsPlaying] = useState(false); const [progress, setProgress] = useState(0); const [currentTime, setCurrentTime] = useState(0); const [audioDuration, setAudioDuration] = useState(0); const audioRef = useRef(null); // Check if we have a valid URL const validUrl = audioUrl && audioUrl.trim() !== ''; useEffect(() => { const audio = audioRef.current; if (audio && validUrl) { // Add loadedmetadata event to get duration const handleLoadedMetadata = () => { setAudioDuration(audio.duration); }; const updateProgress = () => { if (audio.duration) { setProgress((audio.currentTime / audio.duration) * 100); setCurrentTime(audio.currentTime); } }; audio.addEventListener('loadedmetadata', handleLoadedMetadata); audio.addEventListener('timeupdate', updateProgress); audio.addEventListener('ended', () => setIsPlaying(false)); return () => { audio.removeEventListener('loadedmetadata', handleLoadedMetadata); audio.removeEventListener('timeupdate', updateProgress); audio.removeEventListener('ended', () => setIsPlaying(false)); }; } }, [audioUrl, validUrl]); const togglePlay = () => { if (audioRef.current && validUrl) { setIsPlaying((prevIsPlaying) => { if (prevIsPlaying) { audioRef.current.pause(); } else { audioRef.current.play().catch(err => { console.error("Error playing audio:", err); setIsPlaying(false); }); } return !prevIsPlaying; }); } }; const formatTime = (time) => { if (isNaN(time) || time < 0) return '0:00'; const minutes = Math.floor(time / 60); const seconds = Math.floor(time % 60); return `${minutes}:${seconds.toString().padStart(2, '0')}`; }; const seekAudio = (event) => { if (!audioRef.current || !validUrl) return; const progressBar = event.currentTarget; const clickX = event.nativeEvent.offsetX; const progressBarWidth = progressBar.clientWidth; // Calculate the new time based on click position const newTime = (clickX / progressBarWidth) * audioRef.current.duration; audioRef.current.currentTime = newTime; // Seek to new time setProgress((newTime / audioRef.current.duration) * 100); }; return (
You haven't generated any podcasts yet. Head over to the Home page to create your first podcast!
{podcast.research ? podcast.research.substring(0, 150) + '...' : 'No description available'}