Skip to content

Commit

Permalink
Prime Youtube and Vimeo players with a blank video
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
cookpete committed Dec 15, 2015
1 parent c6a727a commit ee63484
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
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

* 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
6 changes: 5 additions & 1 deletion src/players/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default class Base extends Component {
static defaultProps = defaultProps
componentDidMount () {
this.update()
if (!this.props.url && this.prime) {
this.priming = true
this.prime()
}
}
componentWillUnmount () {
this.stop()
Expand Down Expand Up @@ -52,7 +56,7 @@ export default class Base extends Component {
}
onReady = () => {
this.setVolume(this.props.volume)
if (this.props.playing) {
if (this.props.playing || this.priming) {
this.play()
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/players/Vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Base from './Base'
const IFRAME_SRC = 'https://player.vimeo.com/video/'
const MATCH_URL = /https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/
const MATCH_MESSAGE_ORIGIN = /^https?:\/\/player.vimeo.com/
const BLANK_VIDEO_URL = 'https://vimeo.com/127250231'
const DEFAULT_IFRAME_PARAMS = {
api: 1,
autoplay: 0,
Expand Down Expand Up @@ -39,6 +40,9 @@ export default class Vimeo extends Base {
this.postMessage('play')
}
}
prime () {
this.play(BLANK_VIDEO_URL)
}
pause () {
this.postMessage('pause')
}
Expand Down
4 changes: 4 additions & 0 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const SDK_URL = '//www.youtube.com/iframe_api'
const SDK_GLOBAL = 'YT'
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,
controls: 0,
Expand Down Expand Up @@ -64,6 +65,9 @@ export default class YouTube extends Base {
if (state.data === YT.PlayerState.BUFFERING) this.props.onBuffer()
if (state.data === YT.PlayerState.ENDED) this.props.onEnded()
}
prime () {
this.play(BLANK_VIDEO_URL)
}
pause () {
if (!this.player) return
this.player.pauseVideo()
Expand Down

0 comments on commit ee63484

Please sign in to comment.