Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const SDK_URL = 'https://www.youtube.com/iframe_api'
const SDK_GLOBAL = 'YT'
const SDK_GLOBAL_READY = 'onYouTubeIframeAPIReady'
const MATCH_URL = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/
const PLAYER_ID = 'youtube-player'
const BLANK_VIDEO_URL = 'https://www.youtube.com/watch?v=GlCmAC4MHek'
const DEFAULT_PLAYER_VARS = {
autoplay: 0,
Expand All @@ -18,14 +17,11 @@ const DEFAULT_PLAYER_VARS = {
iv_load_policy: 3
}

let playerIdCount = 0

export default class YouTube extends Base {
static displayName = 'YouTube'
static canPlay (url) {
return MATCH_URL.test(url)
}
playerId = PLAYER_ID + '-' + playerIdCount++
componentDidMount () {
const { url, youtubeConfig } = this.props
if (!url && youtubeConfig.preload) {
Expand Down Expand Up @@ -65,7 +61,7 @@ export default class YouTube extends Base {
}
this.loadingSDK = true
this.getSDK().then(YT => {
this.player = new YT.Player(this.playerId, {
this.player = new YT.Player(this.refNode, {
width: '100%',
height: '100%',
videoId: id,
Expand Down Expand Up @@ -136,14 +132,17 @@ export default class YouTube extends Base {
if (!this.isReady || !this.player.getVideoLoadedFraction) return null
return this.player.getVideoLoadedFraction()
}
setRefNode = (node) => {
this.refNode = node
}
render () {
const style = {
height: '100%',
display: this.props.url ? 'block' : 'none'
}
return (
<div style={style}>
<div id={this.playerId} />
<div ref={this.setRefNode} />
</div>
)
}
Expand Down