-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathReactPlayer.js
116 lines (112 loc) · 3.29 KB
/
ReactPlayer.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
import React, { Component } from 'react'
import { propTypes, defaultProps, DEPRECATED_CONFIG_PROPS } from './props'
import { getConfig, omit, isEqual } from './utils'
import players from './players'
import Player from './Player'
import { FilePlayer } from './players/FilePlayer'
import renderPreloadPlayers from './preload'
const SUPPORTED_PROPS = Object.keys(propTypes)
let customPlayers = []
export default class ReactPlayer extends Component {
static addCustomPlayer = player => {
customPlayers.push(player)
}
static removeCustomPlayers = () => {
customPlayers = []
}
static displayName = 'ReactPlayer'
static propTypes = propTypes
static defaultProps = defaultProps
static canPlay = url => {
for (let Player of [ ...customPlayers, ...players ]) {
if (Player.canPlay(url)) {
return true
}
}
return false
}
config = getConfig(this.props, defaultProps, true)
componentDidMount () {
if (this.props.progressFrequency) {
const message = 'ReactPlayer: %cprogressFrequency%c is deprecated, please use %cprogressInterval%c instead'
console.warn(message, 'font-weight: bold', '', 'font-weight: bold', '')
}
}
shouldComponentUpdate (nextProps) {
return !isEqual(this.props, nextProps)
}
componentWillUpdate (nextProps) {
this.config = getConfig(nextProps, defaultProps)
}
getDuration = () => {
if (!this.player) return null
return this.player.getDuration()
}
getCurrentTime = () => {
if (!this.player) return null
return this.player.getCurrentTime()
}
getSecondsLoaded = () => {
if (!this.player) return null
return this.player.getSecondsLoaded()
}
getInternalPlayer = (key = 'player') => {
if (!this.player) return null
return this.player.getInternalPlayer(key)
}
seekTo = fraction => {
if (!this.player) return null
this.player.seekTo(fraction)
}
onReady = () => {
this.props.onReady(this)
}
getActivePlayer (url) {
for (let Player of [ ...customPlayers, ...players ]) {
if (Player.canPlay(url)) {
return Player
}
}
// Fall back to FilePlayer if nothing else can play the URL
return FilePlayer
}
wrapperRef = wrapper => {
this.wrapper = wrapper
}
activePlayerRef = player => {
this.player = player
}
renderActivePlayer (url) {
if (!url) return null
const activePlayer = this.getActivePlayer(url)
return (
<Player
{...this.props}
key={activePlayer.displayName}
ref={this.activePlayerRef}
config={this.config}
activePlayer={activePlayer}
onReady={this.onReady}
/>
)
}
sortPlayers (a, b) {
// Retain player order to prevent weird iframe behaviour when switching players
if (a && b) {
return a.key < b.key ? -1 : 1
}
return 0
}
render () {
const { url, style, width, height, wrapper: Wrapper } = this.props
const otherProps = omit(this.props, SUPPORTED_PROPS, DEPRECATED_CONFIG_PROPS)
const activePlayer = this.renderActivePlayer(url)
const preloadPlayers = renderPreloadPlayers(url, this.config)
const players = [ activePlayer, ...preloadPlayers ].sort(this.sortPlayers)
return (
<Wrapper ref={this.wrapperRef} style={{ ...style, width, height }} {...otherProps}>
{players}
</Wrapper>
)
}
}