-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactPlayer.js
152 lines (147 loc) · 4.51 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
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
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 Preview from './Preview'
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
}
static canEnablePIP = url => {
for (let Player of [ ...customPlayers, ...players ]) {
if (Player.canEnablePIP && Player.canEnablePIP(url)) {
return true
}
}
return false
}
config = getConfig(this.props, defaultProps, true)
state = {
showPreview: !!this.props.light
}
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, nextState) {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState)
}
componentWillUpdate (nextProps) {
this.config = getConfig(nextProps, defaultProps)
}
onClickPreview = () => {
this.setState({ showPreview: false })
}
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, activePlayer) {
if (!url) return null
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, light, wrapper: Wrapper } = this.props
const { showPreview } = this.state
const otherProps = omit(this.props, SUPPORTED_PROPS, DEPRECATED_CONFIG_PROPS)
const activePlayer = this.getActivePlayer(url)
const renderedActivePlayer = this.renderActivePlayer(url, activePlayer)
const preloadPlayers = renderPreloadPlayers(url, this.config)
const players = [ renderedActivePlayer, ...preloadPlayers ].sort(this.sortPlayers)
if (showPreview && url) {
return (
<Preview
url={url}
light={light}
onClick={this.onClickPreview}
/>
)
}
return (
<Wrapper ref={this.wrapperRef} style={{ ...style, width, height }} {...otherProps}>
{players}
</Wrapper>
)
}
}
export { default as YouTube } from './players/YouTube'
export { default as SoundCloud } from './players/SoundCloud'
export { default as Vimeo } from './players/Vimeo'
export { default as Facebook } from './players/Facebook'
export { default as Streamable } from './players/Streamable'
export { default as Wistia } from './players/Wistia'
export { default as Twitch } from './players/Twitch'
export { default as DailyMotion } from './players/DailyMotion'
export { default as Mixcloud } from './players/Mixcloud'
export { default as FilePlayer } from './players/FilePlayer'