File size: 982 Bytes
891fa77 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import { useRef, useState } from 'react'
import type { TimelinePoint } from './api'
export function useTimelinePlayer() {
const audioRef = useRef<HTMLAudioElement | null>(null)
const [currentTime, setCurrentTime] = useState(0)
const [selectedIndex, setSelectedIndex] = useState<number | null>(null)
const selectWindow = (point: TimelinePoint) => {
setSelectedIndex(point.index)
if (audioRef.current) audioRef.current.currentTime = point.start_seconds
setCurrentTime(point.start_seconds)
}
const seek = (time: number) => {
if (audioRef.current) audioRef.current.currentTime = time
setCurrentTime(time)
setSelectedIndex(null)
}
const reset = () => {
audioRef.current?.pause()
if (audioRef.current) audioRef.current.currentTime = 0
setCurrentTime(0)
setSelectedIndex(null)
}
return { audioRef, currentTime, selectedIndex, selectWindow, seek, reset, onTimeUpdate: () => setCurrentTime(audioRef.current?.currentTime ?? 0) }
}
|