-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
YouTube.js
138 lines (132 loc) · 3.74 KB
/
YouTube.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
import React, { Component } from 'react'
import { callPlayer, getSDK, parseStartTime, parseEndTime } from '../utils'
import createSinglePlayer from '../singlePlayer'
const SDK_URL = 'https://www.youtube.com/iframe_api'
const SDK_GLOBAL = 'YT'
const SDK_GLOBAL_READY = 'onYouTubeIframeAPIReady'
const MATCH_URL = /(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})|youtube\.com\/playlist\?list=/
const MATCH_PLAYLIST = /list=([a-zA-Z0-9_-]+)/
function parsePlaylist (url) {
if (MATCH_PLAYLIST.test(url)) {
const [, playlistId] = url.match(MATCH_PLAYLIST)
console.log(playlistId)
return {
listType: 'playlist',
list: playlistId
}
}
return {}
}
export class YouTube extends Component {
static displayName = 'YouTube'
static canPlay = url => MATCH_URL.test(url)
callPlayer = callPlayer
load (url, isReady) {
const { playing, muted, playsinline, controls, loop, config, onError } = this.props
const { playerVars } = config.youtube
const id = url && url.match(MATCH_URL)[1]
if (isReady) {
this.player.cueVideoById({
videoId: id,
startSeconds: parseStartTime(url) || playerVars.start,
endSeconds: parseEndTime(url) || playerVars.end
})
return
}
getSDK(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY, YT => YT.loaded).then(YT => {
if (!this.container) return
this.player = new YT.Player(this.container, {
width: '100%',
height: '100%',
videoId: id,
playerVars: {
autoplay: playing ? 1 : 0,
mute: muted ? 1 : 0,
controls: controls ? 1 : 0,
start: parseStartTime(url),
end: parseEndTime(url),
origin: window.location.origin,
playsinline: playsinline,
...parsePlaylist(url),
...playerVars
},
events: {
onReady: this.props.onReady,
onStateChange: this.onStateChange,
onError: event => onError(event.data)
}
})
if (loop) {
this.player.setLoop(true) // Enable playlist looping
}
}, onError)
}
onStateChange = ({ data }) => {
const { onPlay, onPause, onBuffer, onEnded, onReady, loop } = this.props
const { PLAYING, PAUSED, BUFFERING, ENDED, CUED } = window[SDK_GLOBAL].PlayerState
if (data === PLAYING) onPlay()
if (data === PAUSED) onPause()
if (data === BUFFERING) onBuffer()
if (data === ENDED) {
const isPlaylist = !!this.callPlayer('getPlaylist')
if (loop && !isPlaylist) {
this.play() // Only loop manually if not playing a playlist
}
onEnded()
}
if (data === CUED) onReady()
}
play () {
this.callPlayer('playVideo')
}
pause () {
this.callPlayer('pauseVideo')
}
stop () {
if (!document.body.contains(this.callPlayer('getIframe'))) return
this.callPlayer('stopVideo')
}
seekTo (amount) {
this.callPlayer('seekTo', amount)
if (!this.props.playing) {
this.pause()
}
}
setVolume (fraction) {
this.callPlayer('setVolume', fraction * 100)
}
mute = () => {
this.callPlayer('mute')
}
unmute = () => {
this.callPlayer('unMute')
}
setPlaybackRate (rate) {
this.callPlayer('setPlaybackRate', rate)
}
getDuration () {
return this.callPlayer('getDuration')
}
getCurrentTime () {
return this.callPlayer('getCurrentTime')
}
getSecondsLoaded () {
return this.callPlayer('getVideoLoadedFraction') * this.getDuration()
}
ref = container => {
this.container = container
}
render () {
const style = {
width: '100%',
height: '100%',
...this.props.style
}
return (
<div style={style}>
<div ref={this.ref} />
</div>
)
}
}
export default createSinglePlayer(YouTube)