Skip to content

Commit

Permalink
Enable multiple YouTube players
Browse files Browse the repository at this point in the history
Fixes #15 and supersedes #16
  • Loading branch information
cookpete committed Dec 24, 2015
1 parent 9240861 commit 882dbe5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. This projec

### Unreleased

* Enable [multiple YouTube players](https://github.com/CookPete/react-player/issues/15)
* Fix [YouTube and Vimeo autoplay bug](https://github.com/CookPete/react-player/issues/7)
* [Full commit list](https://github.com/CookPete/react-player/compare/v0.2.1...master)

Expand Down
9 changes: 7 additions & 2 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ const DEFAULT_PLAYER_VARS = {
showinfo: 0
}

let count = 0

export default class YouTube extends Base {
static propTypes = propTypes
static defaultProps = defaultProps
static canPlay (url) {
return MATCH_URL.test(url)
}
playerId = PLAYER_ID + '-' + count++
componentDidMount () {
if (!this.props.url && this.props.youtubeConfig.preload) {
this.preloading = true
Expand All @@ -33,7 +36,9 @@ export default class YouTube extends Base {
return Promise.resolve(window[SDK_GLOBAL])
}
return new Promise((resolve, reject) => {
const previousOnReady = window.onYouTubeIframeAPIReady
window.onYouTubeIframeAPIReady = function () {
if (previousOnReady) previousOnReady()
resolve(window[SDK_GLOBAL])
}
loadScript(SDK_URL, err => {
Expand All @@ -53,7 +58,7 @@ export default class YouTube extends Base {
return
}
this.getSDK().then(YT => {
this.player = new YT.Player(PLAYER_ID, {
this.player = new YT.Player(this.playerId, {
width: '100%',
height: '100%',
videoId: id,
Expand Down Expand Up @@ -103,6 +108,6 @@ export default class YouTube extends Base {
}
render () {
const style = { display: this.props.url ? 'block' : 'none' }
return <div id={PLAYER_ID} style={style} />
return <div id={this.playerId} style={style} />
}
}

0 comments on commit 882dbe5

Please sign in to comment.