|
|
|
|
@ -19,24 +19,30 @@ import { usePlayingHandlers } from './usePlayingHandlers' |
|
|
|
|
import { useVideoQuality } from './useVideoQuality' |
|
|
|
|
import { useDuration } from './useDuration' |
|
|
|
|
import type { Chapters } from '../types' |
|
|
|
|
import { Players } from '../types' |
|
|
|
|
|
|
|
|
|
export type PlayerState = { |
|
|
|
|
activeChapterIndex: number, |
|
|
|
|
activePlayer: Players, |
|
|
|
|
loadedProgress: number, |
|
|
|
|
playedProgress: number, |
|
|
|
|
playing: boolean, |
|
|
|
|
ready: boolean, |
|
|
|
|
seek: number, |
|
|
|
|
seek: Record<Players, number>, |
|
|
|
|
seeking: boolean, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const initialState: PlayerState = { |
|
|
|
|
activeChapterIndex: 0, |
|
|
|
|
activePlayer: 0, |
|
|
|
|
loadedProgress: 0, |
|
|
|
|
playedProgress: 0, |
|
|
|
|
playing: false, |
|
|
|
|
ready: false, |
|
|
|
|
seek: 0, |
|
|
|
|
seek: { |
|
|
|
|
[Players.PLAYER1]: 0, |
|
|
|
|
[Players.PLAYER2]: 0, |
|
|
|
|
}, |
|
|
|
|
seeking: false, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -59,6 +65,7 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
const [ |
|
|
|
|
{ |
|
|
|
|
activeChapterIndex, |
|
|
|
|
activePlayer, |
|
|
|
|
loadedProgress, |
|
|
|
|
playedProgress, |
|
|
|
|
playing, |
|
|
|
|
@ -68,7 +75,8 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
}, |
|
|
|
|
setPlayerState, |
|
|
|
|
] = useObjectState({ ...initialState, activeChapterIndex: resumeFrom.half }) |
|
|
|
|
const videoRef = useRef<HTMLVideoElement>(null) |
|
|
|
|
const video1Ref = useRef<HTMLVideoElement>(null) |
|
|
|
|
const video2Ref = useRef<HTMLVideoElement>(null) |
|
|
|
|
const { |
|
|
|
|
onReady, |
|
|
|
|
playNextChapter, |
|
|
|
|
@ -92,20 +100,24 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
}, [onError, stopPlaying]) |
|
|
|
|
|
|
|
|
|
const onProgressChange = useProgressChangeHandler({ |
|
|
|
|
activeChapterIndex, |
|
|
|
|
chapters, |
|
|
|
|
duration, |
|
|
|
|
setPlayerState, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const getActiveChapterUrl = useCallback((quality: string = selectedQuality) => ( |
|
|
|
|
chapters[activeChapterIndex].urls[quality] |
|
|
|
|
), [selectedQuality, chapters, activeChapterIndex]) |
|
|
|
|
const getChapterUrl = useCallback((index: number, quality: string = selectedQuality) => ( |
|
|
|
|
chapters[index]?.urls[quality] |
|
|
|
|
), [selectedQuality, chapters]) |
|
|
|
|
|
|
|
|
|
const videoRef = [video1Ref, video2Ref][activePlayer] |
|
|
|
|
|
|
|
|
|
const onQualitySelect = (quality: string) => { |
|
|
|
|
setPlayerState({ |
|
|
|
|
seek: videoRef.current?.currentTime ?? 0, |
|
|
|
|
}) |
|
|
|
|
setPlayerState((state) => ({ |
|
|
|
|
seek: { |
|
|
|
|
...state.seek, |
|
|
|
|
[state.activePlayer]: videoRef.current?.currentTime ?? 0, |
|
|
|
|
}, |
|
|
|
|
})) |
|
|
|
|
setSelectedQuality(quality) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -159,12 +171,14 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
activeChapterIndex, |
|
|
|
|
activeSrc: getActiveChapterUrl(), |
|
|
|
|
activePlayer, |
|
|
|
|
activeSrc: getChapterUrl(activeChapterIndex), |
|
|
|
|
chapters, |
|
|
|
|
duration, |
|
|
|
|
isFirstChapterPlaying: activeChapterIndex === 0, |
|
|
|
|
isLastChapterPlaying: activeChapterIndex === numberOfChapters - 1, |
|
|
|
|
loadedProgress, |
|
|
|
|
nextSrc: getChapterUrl(activeChapterIndex + 1), |
|
|
|
|
onError: handleError, |
|
|
|
|
onLoadedProgress, |
|
|
|
|
onPlayedProgress, |
|
|
|
|
@ -181,8 +195,9 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
selectedQuality, |
|
|
|
|
startPlaying, |
|
|
|
|
togglePlaying, |
|
|
|
|
video1Ref, |
|
|
|
|
video2Ref, |
|
|
|
|
videoQualities, |
|
|
|
|
videoRef, |
|
|
|
|
...useFullscreen(), |
|
|
|
|
...useVolume(), |
|
|
|
|
} |
|
|
|
|
|