-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Player.js
256 lines (233 loc) · 6.91 KB
/
Player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { Component } from 'react'
import isEqual from 'react-fast-compare'
import { propTypes, defaultProps } from './props'
const SEEK_ON_PLAY_EXPIRY = 5000
export default class Player extends Component {
static displayName = 'Player'
static propTypes = propTypes
static defaultProps = defaultProps
mounted = false
isReady = false
isPlaying = false // Track playing state internally to prevent bugs
isLoading = true // Use isLoading to prevent onPause when switching URL
loadOnReady = null
startOnPlay = true
seekOnPlay = null
onDurationCalled = false
componentDidMount () {
this.mounted = true
}
componentWillUnmount () {
clearTimeout(this.progressTimeout)
clearTimeout(this.durationCheckTimeout)
if (this.isReady && this.props.stopOnUnmount) {
this.player.stop()
if (this.player.disablePIP) {
this.player.disablePIP()
}
}
this.mounted = false
}
componentDidUpdate (prevProps) {
// Invoke player methods based on changed props
const { url, playing, volume, muted, playbackRate, pip, loop, activePlayer } = this.props
if (!isEqual(prevProps.url, url)) {
if (this.isLoading && !activePlayer.forceLoad) {
console.warn(`ReactPlayer: the attempt to load ${url} is being deferred until the player has loaded`)
this.loadOnReady = url
return
}
this.isLoading = true
this.startOnPlay = true
this.onDurationCalled = false
this.player.load(url, this.isReady)
}
if (!prevProps.playing && playing && !this.isPlaying) {
this.player.play()
}
if (prevProps.playing && !playing && this.isPlaying) {
this.player.pause()
}
if (!prevProps.pip && pip && this.player.enablePIP) {
this.player.enablePIP()
}
if (prevProps.pip && !pip && this.player.disablePIP) {
this.player.disablePIP()
}
if (prevProps.volume !== volume && volume !== null) {
this.player.setVolume(volume)
}
if (prevProps.muted !== muted) {
if (muted) {
this.player.mute()
} else {
this.player.unmute()
if (volume !== null) {
// Set volume next tick to fix a bug with DailyMotion
setTimeout(() => this.player.setVolume(volume))
}
}
}
if (prevProps.playbackRate !== playbackRate && this.player.setPlaybackRate) {
this.player.setPlaybackRate(playbackRate)
}
if (prevProps.loop !== loop && this.player.setLoop) {
this.player.setLoop(loop)
}
}
handlePlayerMount = player => {
this.player = player
this.player.load(this.props.url)
this.progress()
}
getDuration () {
if (!this.isReady) return null
return this.player.getDuration()
}
getCurrentTime () {
if (!this.isReady) return null
return this.player.getCurrentTime()
}
getSecondsLoaded () {
if (!this.isReady) return null
return this.player.getSecondsLoaded()
}
getInternalPlayer = (key) => {
if (!this.player) return null
return this.player[key]
}
progress = () => {
if (this.props.url && this.player && this.isReady) {
const playedSeconds = this.getCurrentTime() || 0
const loadedSeconds = this.getSecondsLoaded()
const duration = this.getDuration()
if (duration) {
const progress = {
playedSeconds,
played: playedSeconds / duration
}
if (loadedSeconds !== null) {
progress.loadedSeconds = loadedSeconds
progress.loaded = loadedSeconds / duration
}
// Only call onProgress if values have changed
if (progress.playedSeconds !== this.prevPlayed || progress.loadedSeconds !== this.prevLoaded) {
this.props.onProgress(progress)
}
this.prevPlayed = progress.playedSeconds
this.prevLoaded = progress.loadedSeconds
}
}
this.progressTimeout = setTimeout(this.progress, this.props.progressFrequency || this.props.progressInterval)
}
seekTo (amount, type) {
// When seeking before player is ready, store value and seek later
if (!this.isReady && amount !== 0) {
this.seekOnPlay = amount
setTimeout(() => { this.seekOnPlay = null }, SEEK_ON_PLAY_EXPIRY)
return
}
const isFraction = !type ? (amount > 0 && amount < 1) : type === 'fraction'
if (isFraction) {
// Convert fraction to seconds based on duration
const duration = this.player.getDuration()
if (!duration) {
console.warn('ReactPlayer: could not seek using fraction – duration not yet available')
return
}
this.player.seekTo(duration * amount)
return
}
this.player.seekTo(amount)
}
handleReady = () => {
if (!this.mounted) return
this.isReady = true
this.isLoading = false
const { onReady, playing, volume, muted } = this.props
onReady()
if (!muted && volume !== null) {
this.player.setVolume(volume)
}
if (this.loadOnReady) {
this.player.load(this.loadOnReady, true)
this.loadOnReady = null
} else if (playing) {
this.player.play()
}
this.handleDurationCheck()
}
handlePlay = () => {
this.isPlaying = true
this.isLoading = false
const { onStart, onPlay, playbackRate } = this.props
if (this.startOnPlay) {
if (this.player.setPlaybackRate && playbackRate !== 1) {
this.player.setPlaybackRate(playbackRate)
}
onStart()
this.startOnPlay = false
}
onPlay()
if (this.seekOnPlay) {
this.seekTo(this.seekOnPlay)
this.seekOnPlay = null
}
this.handleDurationCheck()
}
handlePause = (e) => {
this.isPlaying = false
if (!this.isLoading) {
this.props.onPause(e)
}
}
handleEnded = () => {
const { activePlayer, loop, onEnded } = this.props
if (activePlayer.loopOnEnded && loop) {
this.seekTo(0)
}
if (!loop) {
this.isPlaying = false
onEnded()
}
}
handleError = (...args) => {
this.isLoading = false
this.props.onError(...args)
}
handleDurationCheck = () => {
clearTimeout(this.durationCheckTimeout)
const duration = this.getDuration()
if (duration) {
if (!this.onDurationCalled) {
this.props.onDuration(duration)
this.onDurationCalled = true
}
} else {
this.durationCheckTimeout = setTimeout(this.handleDurationCheck, 100)
}
}
handleLoaded = () => {
// Sometimes we know loading has stopped but onReady/onPlay are never called
// so this provides a way for players to avoid getting stuck
this.isLoading = false
}
render () {
const Player = this.props.activePlayer
if (!Player) {
return null
}
return (
<Player
{...this.props}
onMount={this.handlePlayerMount}
onReady={this.handleReady}
onPlay={this.handlePlay}
onPause={this.handlePause}
onEnded={this.handleEnded}
onLoaded={this.handleLoaded}
onError={this.handleError}
/>
)
}
}