Ott 902 livematch start watch from (#348)

* feat(ott-902): add resume and changed config

* feat(ott-902): fix line
keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
Zoia 5 years ago committed by GitHub
parent e562e8d5cd
commit dcabd96b10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/features/MatchPage/components/LiveMatch/hooks/index.tsx
  2. 4
      src/features/MatchPage/components/LiveMatch/index.tsx
  3. 28
      src/features/MatchPage/hooks/useLastPlayPosition.tsx
  4. 23
      src/features/MatchPage/hooks/useUrlParam.tsx
  5. 6
      src/features/MatchPopup/components/LiveMatchPlaylist/index.tsx
  6. 5
      src/features/QueryParamsStorage/index.tsx
  7. 8
      src/features/StreamPlayer/hooks/index.tsx
  8. 12
      src/features/StreamPlayer/hooks/useHlsPlayer.tsx

@ -10,10 +10,12 @@ import {
import { usePlayerProgressReporter } from 'features/MatchPage/hooks/usePlayerProgressReporter' import { usePlayerProgressReporter } from 'features/MatchPage/hooks/usePlayerProgressReporter'
import { useLastPlayPosition } from 'features/MatchPage/hooks/useLastPlayPosition' import { useLastPlayPosition } from 'features/MatchPage/hooks/useLastPlayPosition'
import { useUrlParam } from 'features/MatchPage/hooks/useUrlParam'
export const useLiveMatch = () => { export const useLiveMatch = () => {
const { sportType } = useSportNameParam() const { sportType } = useSportNameParam()
const matchId = usePageId() const matchId = usePageId()
const resume = useUrlParam()
const [liveVideos, setLiveVideos] = useState<LiveVideos>([]) const [liveVideos, setLiveVideos] = useState<LiveVideos>([])
@ -24,6 +26,7 @@ export const useLiveMatch = () => {
return { return {
matchUrl: liveVideos[0] || '', matchUrl: liveVideos[0] || '',
resume,
...usePlayerProgressReporter(), ...usePlayerProgressReporter(),
...useLastPlayPosition(), ...useLastPlayPosition(),
} }

@ -15,10 +15,10 @@ type Props = {
export const LiveMatch = ({ profile }: Props) => { export const LiveMatch = ({ profile }: Props) => {
const { const {
lastPlayPosition,
matchUrl, matchUrl,
onPlayerProgressChange, onPlayerProgressChange,
onPlayingChange, onPlayingChange,
resume,
} = useLiveMatch() } = useLiveMatch()
return ( return (
@ -29,7 +29,7 @@ export const LiveMatch = ({ profile }: Props) => {
url={matchUrl} url={matchUrl}
onPlayingChange={onPlayingChange} onPlayingChange={onPlayingChange}
onProgressChange={onPlayerProgressChange} onProgressChange={onPlayerProgressChange}
resumeFrom={lastPlayPosition.second} resumeFrom={resume}
/> />
</Container> </Container>
<LiveMatchSidePlaylists /> <LiveMatchSidePlaylists />

@ -1,11 +1,4 @@
import { import { useEffect, useState } from 'react'
useEffect,
useState,
useMemo,
} from 'react'
import { useLocation } from 'react-router'
import isBoolean from 'lodash/isBoolean'
import type { LastPlayPosition } from 'requests' import type { LastPlayPosition } from 'requests'
import { getMatchLastWatchSeconds } from 'requests' import { getMatchLastWatchSeconds } from 'requests'
@ -16,24 +9,12 @@ import {
useRequest, useRequest,
} from 'hooks' } from 'hooks'
export const RESUME_KEY = 'resume'
const readResumeParam = (search: string) => {
const params = new URLSearchParams(search)
const rawValue = params.get(RESUME_KEY)
if (!rawValue) return false
const value = JSON.parse(rawValue)
return isBoolean(value) && Boolean(value)
}
const initialPosition = { const initialPosition = {
half: 0, half: 0,
second: 0, second: 0,
} }
export const useLastPlayPosition = () => { export const useLastPlayPosition = () => {
const { search } = useLocation()
const { sportType } = useSportNameParam() const { sportType } = useSportNameParam()
const matchId = usePageId() const matchId = usePageId()
const [ const [
@ -45,16 +26,11 @@ export const useLastPlayPosition = () => {
request: requestLastPlayPosition, request: requestLastPlayPosition,
} = useRequest(getMatchLastWatchSeconds) } = useRequest(getMatchLastWatchSeconds)
const resume = useMemo(() => readResumeParam(search), [search])
useEffect(() => { useEffect(() => {
if (resume) { requestLastPlayPosition(sportType, matchId).then(setPosition)
requestLastPlayPosition(sportType, matchId).then(setPosition)
}
}, [ }, [
sportType, sportType,
matchId, matchId,
resume,
requestLastPlayPosition, requestLastPlayPosition,
]) ])

@ -0,0 +1,23 @@
import { useMemo } from 'react'
import { useLocation } from 'react-router'
import isNumber from 'lodash/isNumber'
export const RESUME_KEY = 'resume'
const readResumeParam = (search: string) => {
const params = new URLSearchParams(search)
const rawValue = params.get(RESUME_KEY)
if (!rawValue) return undefined
const value = JSON.parse(rawValue)
return isNumber(value) ? value : 0
}
export const useUrlParam = () => {
const { search } = useLocation()
const resume = useMemo(() => readResumeParam(search), [search])
return resume
}

@ -47,13 +47,13 @@ export const LiveMatchPlaylist = () => {
<List> <List>
<Item> <Item>
<SimplePlaylistButton <SimplePlaylistButton
to={`${sport}${PAGES.match}/${match.id}`} to={`/${sport}${PAGES.match}/${match.id}`}
title='watch_live_stream' title='watch_live_stream'
/> />
</Item> </Item>
<Item> <Item>
<SimplePlaylistButton <SimplePlaylistButton
to={`${sport}${PAGES.match}/${match.id}`} to={`/${sport}${PAGES.match}/${match.id}/?resume=${0}`}
title='watch_from_beginning' title='watch_from_beginning'
/> />
</Item> </Item>
@ -61,7 +61,7 @@ export const LiveMatchPlaylist = () => {
? ( ? (
<Item> <Item>
<SimplePlaylistButton <SimplePlaylistButton
to={`${sport}${PAGES.match}/${match.id}`} to={`/${sport}${PAGES.match}/${match.id}/?resume=${lastPlayPosition.second}`}
title='watch_from' title='watch_from'
startWatch={lastPlayPosition?.second} startWatch={lastPlayPosition?.second}
/> />

@ -22,6 +22,11 @@ class QueryParamStorage implements Storage {
constructor(historyArg: History) { constructor(historyArg: History) {
this.history = historyArg this.history = historyArg
this.urlParams = new URLSearchParams(this.history.location.search) this.urlParams = new URLSearchParams(this.history.location.search)
this.history.listen((location, action) => {
if (action !== 'REPLACE') {
this.urlParams = new URLSearchParams(location.search)
}
})
} }
get entries() { get entries() {

@ -26,7 +26,7 @@ const initialState = {
export type Props = { export type Props = {
onPlayingChange: (playing: boolean) => void, onPlayingChange: (playing: boolean) => void,
onProgressChange: (seconds: number) => void, onProgressChange: (seconds: number) => void,
resumeFrom: number, resumeFrom?: number,
url: string, url: string,
} }
@ -36,7 +36,7 @@ export const useVideoPlayer = ({
resumeFrom, resumeFrom,
url, url,
}: Props) => { }: Props) => {
const { hls, videoRef } = useHlsPlayer(url) const { hls, videoRef } = useHlsPlayer(url, resumeFrom)
const [{ const [{
duration, duration,
loadedProgress, loadedProgress,
@ -46,8 +46,8 @@ export const useVideoPlayer = ({
seek, seek,
}, setPlayerState] = useObjectState({ }, setPlayerState] = useObjectState({
...initialState, ...initialState,
playedProgress: toMilliSeconds(resumeFrom), playedProgress: toMilliSeconds(resumeFrom || 0),
seek: resumeFrom, seek: resumeFrom || 0,
}) })
const startPlaying = useMemo(() => once(() => { const startPlaying = useMemo(() => once(() => {

@ -5,10 +5,18 @@ import {
} from 'react' } from 'react'
import Hls from 'hls.js' import Hls from 'hls.js'
import isNumber from 'lodash/isNumber'
import { streamConfig } from '../config' import { streamConfig } from '../config'
export const useHlsPlayer = (src: string) => { export const useHlsPlayer = (src: string, resumeFrom?: number) => {
const hls = useMemo(() => new Hls(streamConfig), []) const hls = useMemo(() => {
const newStreamConfig = { ...streamConfig }
if (isNumber(resumeFrom)) {
newStreamConfig.startPosition = resumeFrom
}
return new Hls(newStreamConfig)
}, [resumeFrom])
const videoRef = useRef<HTMLVideoElement>(null) const videoRef = useRef<HTMLVideoElement>(null)
useEffect(() => { useEffect(() => {

Loading…
Cancel
Save