From 6e7af920b85c0af2f65b214689aae504497c8d72 Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Wed, 22 Jun 2022 21:32:35 +0700 Subject: [PATCH] fix(#1701): fix button live for live stream from episodes --- src/features/StreamPlayer/hooks/index.tsx | 61 ++++++++++++----------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/features/StreamPlayer/hooks/index.tsx b/src/features/StreamPlayer/hooks/index.tsx index c3b7b835..c625847d 100644 --- a/src/features/StreamPlayer/hooks/index.tsx +++ b/src/features/StreamPlayer/hooks/index.tsx @@ -81,11 +81,11 @@ export const useVideoPlayer = ({ }, setPlayerState] = useObjectState({ ...initialState, chapters: chaptersProps }) const { onPlaylistSelect, selectedPlaylist } = useLiveMatch() - const [pauseTime, setPauseTime] = useState(0) const { url } = chapters[0] ?? { url: '' } const numberOfChapters = size(chapters) const { hls, videoRef } = useHlsPlayer(url, resumeFrom) + const [isLiveTime, setIsLiveTime] = useState(false) const chaptersDuration = useDuration(chapters) @@ -100,7 +100,6 @@ export const useVideoPlayer = ({ stopPlaying, togglePlaying, } = usePlayingHandlers(setPlayerState, chapters) - console.log('playedProgress', playedProgress, '|', 'chapters =', chapters) const getActiveChapter = useCallback( (index: number = activeChapterIndex) => chapters[index], @@ -205,10 +204,35 @@ export const useVideoPlayer = ({ } const backToLive = useCallback(() => { - const liveProgressMs = Math.max(duration - 30000, 0) + if (!duration) return + if (selectedPlaylist?.id !== 'full_game') { + onPlaylistSelect({ + duration: 0, + episodes: [], + id: 'full_game', + lexic: 13028, + type: 0, + }) + setIsLiveTime(true) + } + + const liveProgressMs = Math.max(duration - 30000, 0) setPlayerState({ playedProgress: liveProgressMs, seek: liveProgressMs / 1000 }) - }, [duration, setPlayerState]) + if (liveProgressMs > 0) setIsLiveTime(false) + }, [ + duration, + onPlaylistSelect, + selectedPlaylist, + setPlayerState, + ]) + + useEffect(() => { + if (duration && isLiveTime && chapters[0]?.isFullMatchChapter) { + backToLive() + } + // eslint-disable-next-line + }, [duration, isLiveTime]) useEventListener({ callback: (e: KeyboardEvent) => { @@ -237,50 +261,27 @@ export const useVideoPlayer = ({ ...initialState, chapters: chaptersProps, playing: true, - seek: (pauseTime || chaptersProps[0].startOffsetMs) / 1000, + seek: chaptersProps[0].startOffsetMs / 1000, }) }, [ chapters, chaptersProps, isLive, setPlayerState, - pauseTime, ]) -console.log('pauseTime', pauseTime, '\n', 'chapters', chapters, '\n', 'playedProgress', playedProgress, '\n', 'selectedPlaylist', selectedPlaylist) - - useEffect(() => { - if (!isLive) return - if (isLive && selectedPlaylist?.id !== 'full_game') { - // setPauseTime(playedProgress) - } - }, [selectedPlaylist]) useEffect(() => { if ((isLive && chapters[0]?.isFullMatchChapter) || isEmpty(chapters)) return const { duration: chapterDuration } = getActiveChapter() - if (playedProgress >= chapterDuration && !seeking && !isLive) { + if (playedProgress >= chapterDuration && !seeking) { playNextChapter() - } else if (playedProgress >= chapterDuration && !seeking && isLive) { - onPlaylistSelect({ - duration: 0, - episodes: [], - id: 'full_game', - lexic: 13028, - type: 0, - }) - console.log('newChapters', chapters) - seekTo(pauseTime) - // setPlayerState({ - // ...initialState, - // chapters: chaptersProps, - // seek: pauseTime / 1000, - // }) } }, [ isLive, chapters, getActiveChapter, + onPlaylistSelect, playedProgress, seeking, playNextChapter,