|
|
|
|
@ -1,12 +1,12 @@ |
|
|
|
|
import type { MouseEvent } from 'react' |
|
|
|
|
import { |
|
|
|
|
useCallback, |
|
|
|
|
useEffect, |
|
|
|
|
useState, |
|
|
|
|
useRef, |
|
|
|
|
} from 'react' |
|
|
|
|
|
|
|
|
|
import size from 'lodash/size' |
|
|
|
|
import isEmpty from 'lodash/isEmpty' |
|
|
|
|
|
|
|
|
|
import type { Videos } from 'requests' |
|
|
|
|
|
|
|
|
|
@ -16,13 +16,10 @@ import { useFullscreen } from 'features/StreamPlayer/hooks/useFullscreen' |
|
|
|
|
import { useProgressChangeHandler } from './useProgressChangeHandler' |
|
|
|
|
import { useLoadedEvent, usePlayedEvent } from './useProgressEvents' |
|
|
|
|
import { usePlayingState } from './usePlayingState' |
|
|
|
|
import { useVideoQuality } from './useVideoQuality' |
|
|
|
|
import { useDuration } from './useDuration' |
|
|
|
|
import { useVolume } from './useVolume' |
|
|
|
|
import { useVideos } from './useVideos' |
|
|
|
|
import { |
|
|
|
|
preparePlayer, |
|
|
|
|
startPlayer, |
|
|
|
|
} from '../helpers' |
|
|
|
|
|
|
|
|
|
export type Props = { |
|
|
|
|
onError?: () => void, |
|
|
|
|
@ -43,31 +40,50 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
const [loadedProgress, setLoadedProgress] = useState(0) |
|
|
|
|
const [playedProgress, setPlayedProgress] = useState(0) |
|
|
|
|
const { |
|
|
|
|
continuePlaying, |
|
|
|
|
firstTimeStart, |
|
|
|
|
playing, |
|
|
|
|
ready, |
|
|
|
|
startPlaying, |
|
|
|
|
stopPlaying, |
|
|
|
|
togglePlaying, |
|
|
|
|
} = usePlayingState(videoRef) |
|
|
|
|
|
|
|
|
|
const { |
|
|
|
|
selectedQuality, |
|
|
|
|
setSelectedQuality, |
|
|
|
|
videoQualities, |
|
|
|
|
} = useVideoQuality(videos) |
|
|
|
|
|
|
|
|
|
const { chapters } = useVideos(videos) |
|
|
|
|
const duration = useDuration(chapters) |
|
|
|
|
|
|
|
|
|
const onProgressChange = useProgressChangeHandler({ |
|
|
|
|
activeChapterIndex, |
|
|
|
|
chapters, |
|
|
|
|
continuePlaying, |
|
|
|
|
duration, |
|
|
|
|
playing, |
|
|
|
|
selectedQuality, |
|
|
|
|
setPlayedProgress, |
|
|
|
|
videoRef, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const getCurrentChapterUrl = useCallback((quality: string = selectedQuality) => ( |
|
|
|
|
chapters[activeChapterIndex.current].urls[quality] |
|
|
|
|
), [selectedQuality, chapters]) |
|
|
|
|
|
|
|
|
|
const onQualitySelect = (quality: string) => { |
|
|
|
|
setSelectedQuality(quality) |
|
|
|
|
continuePlaying(getCurrentChapterUrl(quality), true) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const playNextChapter = () => { |
|
|
|
|
activeChapterIndex.current += 1 |
|
|
|
|
const isLastChapterPlayed = activeChapterIndex.current === size(chapters) |
|
|
|
|
if (isLastChapterPlayed) { |
|
|
|
|
activeChapterIndex.current = 0 |
|
|
|
|
preparePlayer(videoRef, chapters[activeChapterIndex.current]) |
|
|
|
|
togglePlaying() |
|
|
|
|
stopPlaying(getCurrentChapterUrl()) |
|
|
|
|
} else { |
|
|
|
|
startPlayer(videoRef, chapters[activeChapterIndex.current]) |
|
|
|
|
continuePlaying(getCurrentChapterUrl()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -92,11 +108,15 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (isEmpty(chapters)) return |
|
|
|
|
|
|
|
|
|
startPlayer(videoRef, chapters[activeChapterIndex.current]) |
|
|
|
|
togglePlaying() |
|
|
|
|
}, [chapters, togglePlaying]) |
|
|
|
|
const url = getCurrentChapterUrl() |
|
|
|
|
if (url && firstTimeStart) { |
|
|
|
|
startPlaying(url) |
|
|
|
|
} |
|
|
|
|
}, [ |
|
|
|
|
firstTimeStart, |
|
|
|
|
getCurrentChapterUrl, |
|
|
|
|
startPlaying, |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
useLoadedEvent({ |
|
|
|
|
onChange: onLoadedChange, |
|
|
|
|
@ -125,13 +145,10 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
}, [playing, onPlayingChange]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (!ready) return |
|
|
|
|
|
|
|
|
|
const progressSeconds = playedProgress / 1000 |
|
|
|
|
const { period } = chapters[activeChapterIndex.current] |
|
|
|
|
onProgressChangeCallback(progressSeconds, Number(period)) |
|
|
|
|
}, [ |
|
|
|
|
ready, |
|
|
|
|
playedProgress, |
|
|
|
|
chapters, |
|
|
|
|
onProgressChangeCallback, |
|
|
|
|
@ -143,9 +160,12 @@ export const useMultiSourcePlayer = ({ |
|
|
|
|
loadedProgress, |
|
|
|
|
onPlayerClick, |
|
|
|
|
onProgressChange, |
|
|
|
|
onQualitySelect, |
|
|
|
|
playedProgress, |
|
|
|
|
playing, |
|
|
|
|
selectedQuality, |
|
|
|
|
togglePlaying, |
|
|
|
|
videoQualities, |
|
|
|
|
videoRef, |
|
|
|
|
wrapperRef, |
|
|
|
|
...useFullscreen(wrapperRef), |
|
|
|
|
|