Skip to content

Commit 882dbe5

Browse files
committed
Enable multiple YouTube players
Fixes #15 and supersedes #16
1 parent 9240861 commit 882dbe5

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. This projec
44

55
### Unreleased
66

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

src/players/YouTube.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ const DEFAULT_PLAYER_VARS = {
1515
showinfo: 0
1616
}
1717

18+
let count = 0
19+
1820
export default class YouTube extends Base {
1921
static propTypes = propTypes
2022
static defaultProps = defaultProps
2123
static canPlay (url) {
2224
return MATCH_URL.test(url)
2325
}
26+
playerId = PLAYER_ID + '-' + count++
2427
componentDidMount () {
2528
if (!this.props.url && this.props.youtubeConfig.preload) {
2629
this.preloading = true
@@ -33,7 +36,9 @@ export default class YouTube extends Base {
3336
return Promise.resolve(window[SDK_GLOBAL])
3437
}
3538
return new Promise((resolve, reject) => {
39+
const previousOnReady = window.onYouTubeIframeAPIReady
3640
window.onYouTubeIframeAPIReady = function () {
41+
if (previousOnReady) previousOnReady()
3742
resolve(window[SDK_GLOBAL])
3843
}
3944
loadScript(SDK_URL, err => {
@@ -53,7 +58,7 @@ export default class YouTube extends Base {
5358
return
5459
}
5560
this.getSDK().then(YT => {
56-
this.player = new YT.Player(PLAYER_ID, {
61+
this.player = new YT.Player(this.playerId, {
5762
width: '100%',
5863
height: '100%',
5964
videoId: id,
@@ -103,6 +108,6 @@ export default class YouTube extends Base {
103108
}
104109
render () {
105110
const style = { display: this.props.url ? 'block' : 'none' }
106-
return <div id={PLAYER_ID} style={style} />
111+
return <div id={this.playerId} style={style} />
107112
}
108113
}

0 commit comments

Comments
 (0)