Skip to content

Commit

Permalink
Loop youtube playlists correctly
Browse files Browse the repository at this point in the history
Fixes cookpete/react-player#530
Instead of setting the `loopOnEnded` static and letting Player loop for us, we
want to manually loop within the Youtube player to ensure that we are _not_
playing a playlist when the video ends, which would loop a single video.
Instead we use `player.setLoop` to ensure playlists loop
  • Loading branch information
happydev829 committed Dec 5, 2018
1 parent e21e4ac commit ac9ddb8
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ const MATCH_URL = /(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?
export class YouTube extends Component {
static displayName = 'YouTube'
static canPlay = url => MATCH_URL.test(url)
static loopOnEnded = true

callPlayer = callPlayer
load (url, isReady) {
const { playing, muted, playsinline, controls, config, onError } = this.props
const { playing, muted, playsinline, controls, loop, config, onError } = this.props
const { playerVars } = config.youtube
const id = url && url.match(MATCH_URL)[1]
if (isReady) {
Expand Down Expand Up @@ -48,15 +47,24 @@ export class YouTube extends Component {
onError: event => onError(event.data)
}
})
if (loop) {
this.player.setLoop(true) // Enable playlist looping
}
}, onError)
}
onStateChange = ({ data }) => {
const { onPlay, onPause, onBuffer, onEnded, onReady } = this.props
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) onEnded()
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 () {
Expand Down

0 comments on commit ac9ddb8

Please sign in to comment.