Fix CORS
This commit is contained in:
@@ -5,10 +5,11 @@ import type { Track } from "@/lib/types"
|
||||
|
||||
interface AudioPlayerProps {
|
||||
track: Track | null
|
||||
isPlaying: boolean
|
||||
onPlayingChange: (playing: boolean) => void
|
||||
}
|
||||
|
||||
export default function AudioPlayer({ track }: AudioPlayerProps) {
|
||||
const [isPlaying, setIsPlaying] = useState(false)
|
||||
export default function AudioPlayer({ track, isPlaying, onPlayingChange }: AudioPlayerProps) {
|
||||
const [currentTime, setCurrentTime] = useState(0)
|
||||
const [duration, setDuration] = useState(0)
|
||||
const [volume, setVolume] = useState(1)
|
||||
@@ -22,7 +23,7 @@ export default function AudioPlayer({ track }: AudioPlayerProps) {
|
||||
// Load audio and waveform when track changes
|
||||
useEffect(() => {
|
||||
if (!track) {
|
||||
setIsPlaying(false)
|
||||
onPlayingChange(false)
|
||||
setCurrentTime(0)
|
||||
setWaveformPeaks([])
|
||||
return
|
||||
@@ -33,13 +34,13 @@ export default function AudioPlayer({ track }: AudioPlayerProps) {
|
||||
|
||||
if (audioRef.current) {
|
||||
audioRef.current.load()
|
||||
// Autoplay when track loads
|
||||
audioRef.current.play().then(() => {
|
||||
setIsPlaying(true)
|
||||
}).catch((error: unknown) => {
|
||||
console.error("Autoplay failed:", error)
|
||||
setIsPlaying(false)
|
||||
})
|
||||
// Autoplay when track loads if isPlaying is true
|
||||
if (isPlaying) {
|
||||
audioRef.current.play().catch((error: unknown) => {
|
||||
console.error("Autoplay failed:", error)
|
||||
onPlayingChange(false)
|
||||
})
|
||||
}
|
||||
}
|
||||
}, [track?.id])
|
||||
|
||||
@@ -54,7 +55,7 @@ export default function AudioPlayer({ track }: AudioPlayerProps) {
|
||||
setDuration(audio.duration)
|
||||
}
|
||||
}
|
||||
const handleEnded = () => setIsPlaying(false)
|
||||
const handleEnded = () => onPlayingChange(false)
|
||||
|
||||
audio.addEventListener("timeupdate", updateTime)
|
||||
audio.addEventListener("loadedmetadata", updateDuration)
|
||||
@@ -91,15 +92,24 @@ export default function AudioPlayer({ track }: AudioPlayerProps) {
|
||||
}
|
||||
}
|
||||
|
||||
const togglePlay = () => {
|
||||
if (!audioRef.current || !track) return
|
||||
// Sync playing state with audio element
|
||||
useEffect(() => {
|
||||
const audio = audioRef.current
|
||||
if (!audio) return
|
||||
|
||||
if (isPlaying) {
|
||||
audioRef.current.pause()
|
||||
audio.play().catch((error: unknown) => {
|
||||
console.error("Play failed:", error)
|
||||
onPlayingChange(false)
|
||||
})
|
||||
} else {
|
||||
audioRef.current.play()
|
||||
audio.pause()
|
||||
}
|
||||
setIsPlaying(!isPlaying)
|
||||
}, [isPlaying, onPlayingChange])
|
||||
|
||||
const togglePlay = () => {
|
||||
if (!audioRef.current || !track) return
|
||||
onPlayingChange(!isPlaying)
|
||||
}
|
||||
|
||||
const handleVolumeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
|
||||
Reference in New Issue
Block a user