@x-edu/live-player 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,148 +1,148 @@
1
- import React, { useState, useEffect, useRef } from 'react'
2
- import { Video } from 'fish'
3
- import dayjs from 'dayjs'
4
- import { PUBLIC_LIVE_STATUS } from '@/config/publicLive'
5
- import { getUrlType, getType } from '@/util/video'
6
- import { getRecordLiveStatus } from '@/util/live'
7
- import LiveStatus from '../LiveStatus'
8
- import style from './index.module.less'
9
-
10
- export default function RecordVideo({
11
- userInfo,
12
- liveInfo,
13
- visitTime,
14
- diffTime,
15
- onStatusChange,
16
- onPlayReplay,
17
- onVideoPlay = () => undefined,
18
- onRequestFullScreen = () => undefined
19
- }) {
20
- const isFullScreenRef = useRef(false)
21
- const [isLiveLoading, setIsLiveLoading] = useState(false)
22
- const [isLiveLoadError, setIsLiveLoadError] = useState(false)
23
- const hasPlayed = useRef(false)
24
- const player = useRef()
25
- const endTimer = useRef()
26
- const needLogin = !userInfo && liveInfo.login
27
-
28
- useEffect(() => {
29
- const isLiving = liveInfo.status === PUBLIC_LIVE_STATUS.LIVEING
30
- if (isLiving && needLogin) {
31
- const timeToEnd = dayjs(liveInfo.end_time).valueOf() - (Date.now() - diffTime)
32
- if (timeToEnd > 0) {
33
- endTimer.current = setTimeout(() => {
34
- onStatusChange(PUBLIC_LIVE_STATUS.COMPLETEED)
35
- }, timeToEnd)
36
- }
37
- }
38
-
39
- if (player.current) {
40
- player.current.controls(isLiving && !needLogin)
41
-
42
- // 显示成封面
43
- if (liveInfo.status === PUBLIC_LIVE_STATUS.COMPLETEED || liveInfo.status === PUBLIC_LIVE_STATUS.OFFLINE) {
44
- player.current.autoplay(false)
45
- player.current.load()
46
- }
47
- }
48
-
49
- return () => {
50
- clearTimeout(endTimer.current)
51
- }
52
- }, [liveInfo.status, userInfo])
53
-
54
- const handleVideoReady = (v, playerIns) => {
55
- player.current = playerIns
56
-
57
- playerIns.on('fullscreenchange', () => {
58
- const isFullScreenNow = playerIns.isFullscreen()
59
- if (isFullScreenNow) {
60
- if (!isFullScreenRef.current) {
61
- onRequestFullScreen()
62
- }
63
- isFullScreenRef.current = true
64
- } else {
65
- isFullScreenRef.current = false
66
- }
67
- })
68
- playerIns.on('waiting', () => {
69
- setIsLiveLoading(true)
70
- })
71
- playerIns.on('playing', () => {
72
- setIsLiveLoading(false)
73
- })
74
- playerIns.on('ended', () => {
75
- onStatusChange(PUBLIC_LIVE_STATUS.COMPLETEED)
76
- })
77
- playerIns.on('play', () => {
78
- const currentTime = Date.now() - diffTime
79
- const status = getRecordLiveStatus(liveInfo.status, currentTime, liveInfo.begin_time, liveInfo.end_time)
80
- onStatusChange(status)
81
- if (status === PUBLIC_LIVE_STATUS.LIVEING) {
82
- player.current.currentTime(Math.max(0, (currentTime - dayjs(liveInfo.begin_time).valueOf()) / 1000))
83
- }
84
-
85
- if (!hasPlayed.current) {
86
- onVideoPlay()
87
- }
88
- hasPlayed.current = true
89
-
90
- player.current.autoplay(false)
91
- setIsLiveLoadError(false)
92
- })
93
- playerIns.on('error', () => {
94
- setIsLiveLoadError(true)
95
- })
96
-
97
- const isLiving = liveInfo.status === PUBLIC_LIVE_STATUS.LIVEING
98
- player.current.controls(isLiving && !needLogin)
99
- if (isLiving) {
100
- const currentTime = Math.max(0, (visitTime - dayjs(liveInfo.begin_time).valueOf()) / 1000)
101
- player.current.currentTime(currentTime)
102
- }
103
- }
104
-
105
- const handleVideoReload = () => {
106
- if (player.current) {
107
- player.current.autoplay(true)
108
- player.current.load()
109
- }
110
- }
111
-
112
- const handleStatusChange = (status, params = {}) => {
113
- onStatusChange(status)
114
- if (status === PUBLIC_LIVE_STATUS.LIVEING && params.play) {
115
- player.current.play()
116
- }
117
- }
118
-
119
- const options = {
120
- sources: [{
121
- src: liveInfo.video_url,
122
- type: getType(getUrlType(liveInfo.video_url))
123
- }],
124
- poster: liveInfo.cover_pic_web_url,
125
- autoplay: false
126
- }
127
-
128
- return (
129
- <div className={style['record-video']}>
130
- <Video
131
- className="vjs-big-play-centered"
132
- options={options}
133
- onReady={handleVideoReady}
134
- />
135
- <LiveStatus
136
- userInfo={userInfo}
137
- visitTime={visitTime}
138
- diffTime={diffTime}
139
- liveInfo={liveInfo}
140
- isLiveLoading={isLiveLoading}
141
- isLiveLoadError={isLiveLoadError}
142
- onStatusChange={handleStatusChange}
143
- onPlayReplay={onPlayReplay}
144
- onReloadLive={handleVideoReload}
145
- />
146
- </div>
147
- )
148
- }
1
+ import React, { useState, useEffect, useRef } from 'react'
2
+ import { Video } from 'fish'
3
+ import dayjs from 'dayjs'
4
+ import { PUBLIC_LIVE_STATUS } from '@/config/publicLive'
5
+ import { getUrlType, getType } from '@/util/video'
6
+ import { getRecordLiveStatus } from '@/util/live'
7
+ import LiveStatus from '../LiveStatus'
8
+ import style from './index.module.less'
9
+
10
+ export default function RecordVideo({
11
+ userInfo,
12
+ liveInfo,
13
+ visitTime,
14
+ diffTime,
15
+ onStatusChange,
16
+ onPlayReplay,
17
+ onVideoPlay = () => undefined,
18
+ onRequestFullScreen = () => undefined
19
+ }) {
20
+ const isFullScreenRef = useRef(false)
21
+ const [isLiveLoading, setIsLiveLoading] = useState(false)
22
+ const [isLiveLoadError, setIsLiveLoadError] = useState(false)
23
+ const hasPlayed = useRef(false)
24
+ const player = useRef()
25
+ const endTimer = useRef()
26
+ const needLogin = !userInfo && liveInfo.login
27
+
28
+ useEffect(() => {
29
+ const isLiving = liveInfo.status === PUBLIC_LIVE_STATUS.LIVEING
30
+ if (isLiving && needLogin) {
31
+ const timeToEnd = dayjs(liveInfo.end_time).valueOf() - (Date.now() - diffTime)
32
+ if (timeToEnd > 0) {
33
+ endTimer.current = setTimeout(() => {
34
+ onStatusChange(PUBLIC_LIVE_STATUS.COMPLETEED)
35
+ }, timeToEnd)
36
+ }
37
+ }
38
+
39
+ if (player.current) {
40
+ player.current.controls(isLiving && !needLogin)
41
+
42
+ // 显示成封面
43
+ if (liveInfo.status === PUBLIC_LIVE_STATUS.COMPLETEED || liveInfo.status === PUBLIC_LIVE_STATUS.OFFLINE) {
44
+ player.current.autoplay(false)
45
+ player.current.load()
46
+ }
47
+ }
48
+
49
+ return () => {
50
+ clearTimeout(endTimer.current)
51
+ }
52
+ }, [liveInfo.status, userInfo])
53
+
54
+ const handleVideoReady = (v, playerIns) => {
55
+ player.current = playerIns
56
+
57
+ playerIns.on('fullscreenchange', () => {
58
+ const isFullScreenNow = playerIns.isFullscreen()
59
+ if (isFullScreenNow) {
60
+ if (!isFullScreenRef.current) {
61
+ onRequestFullScreen()
62
+ }
63
+ isFullScreenRef.current = true
64
+ } else {
65
+ isFullScreenRef.current = false
66
+ }
67
+ })
68
+ playerIns.on('waiting', () => {
69
+ setIsLiveLoading(true)
70
+ })
71
+ playerIns.on('playing', () => {
72
+ setIsLiveLoading(false)
73
+ })
74
+ playerIns.on('ended', () => {
75
+ onStatusChange(PUBLIC_LIVE_STATUS.COMPLETEED)
76
+ })
77
+ playerIns.on('play', () => {
78
+ const currentTime = Date.now() - diffTime
79
+ const status = getRecordLiveStatus(liveInfo.status, currentTime, liveInfo.begin_time, liveInfo.end_time)
80
+ onStatusChange(status)
81
+ if (status === PUBLIC_LIVE_STATUS.LIVEING) {
82
+ player.current.currentTime(Math.max(0, (currentTime - dayjs(liveInfo.begin_time).valueOf()) / 1000))
83
+ }
84
+
85
+ if (!hasPlayed.current) {
86
+ onVideoPlay()
87
+ }
88
+ hasPlayed.current = true
89
+
90
+ player.current.autoplay(false)
91
+ setIsLiveLoadError(false)
92
+ })
93
+ playerIns.on('error', () => {
94
+ setIsLiveLoadError(true)
95
+ })
96
+
97
+ const isLiving = liveInfo.status === PUBLIC_LIVE_STATUS.LIVEING
98
+ player.current.controls(isLiving && !needLogin)
99
+ if (isLiving) {
100
+ const currentTime = Math.max(0, (visitTime - dayjs(liveInfo.begin_time).valueOf()) / 1000)
101
+ player.current.currentTime(currentTime)
102
+ }
103
+ }
104
+
105
+ const handleVideoReload = () => {
106
+ if (player.current) {
107
+ player.current.autoplay(true)
108
+ player.current.load()
109
+ }
110
+ }
111
+
112
+ const handleStatusChange = (status, params = {}) => {
113
+ onStatusChange(status)
114
+ if (status === PUBLIC_LIVE_STATUS.LIVEING && params.play) {
115
+ player.current.play()
116
+ }
117
+ }
118
+
119
+ const options = {
120
+ sources: [{
121
+ src: liveInfo.video_url,
122
+ type: getType(getUrlType(liveInfo.video_url))
123
+ }],
124
+ poster: liveInfo.cover_pic_web_url,
125
+ autoplay: false
126
+ }
127
+
128
+ return (
129
+ <div className={style['record-video']}>
130
+ <Video
131
+ className="vjs-big-play-centered"
132
+ options={options}
133
+ onReady={handleVideoReady}
134
+ />
135
+ <LiveStatus
136
+ userInfo={userInfo}
137
+ visitTime={visitTime}
138
+ diffTime={diffTime}
139
+ liveInfo={liveInfo}
140
+ isLiveLoading={isLiveLoading}
141
+ isLiveLoadError={isLiveLoadError}
142
+ onStatusChange={handleStatusChange}
143
+ onPlayReplay={onPlayReplay}
144
+ onReloadLive={handleVideoReload}
145
+ />
146
+ </div>
147
+ )
148
+ }