From ac9ddb8e9fb4e17a6b279ef657d237f6d9a4c6b2 Mon Sep 17 00:00:00 2001 From: happydev829 Date: Wed, 5 Dec 2018 14:44:32 +0000 Subject: [PATCH] Loop youtube playlists correctly Fixes https://github.com/CookPete/react-player/issues/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 --- src/players/YouTube.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/players/YouTube.js b/src/players/YouTube.js index a0b904b..c3377cd 100644 --- a/src/players/YouTube.js +++ b/src/players/YouTube.js @@ -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) { @@ -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 () {