Skip to content

Commit

Permalink
Support array of youtube urls
Browse files Browse the repository at this point in the history
  • Loading branch information
webmiraclepro committed May 31, 2020
1 parent 2fb4683 commit 4f8cfaa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ Due to various restrictions, `ReactPlayer` is not guaranteed to function properl
#### Multiple Sources and Tracks

Passing an array of YouTube URLs to the `url` prop will load them as an untitled playlist.

```jsx
<ReactPlayer
url={[
'https://www.youtube.com/watch?v=oUFJJNQGwhk',
'https://www.youtube.com/watch?v=jNgP6d9HraI'
]}
/>
```

When playing file paths, an array of sources can be passed to the `url` prop to render multiple `<source>` tags.

```jsx
Expand Down
55 changes: 34 additions & 21 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,6 @@ const MATCH_USER_UPLOADS = /user\/([a-zA-Z0-9_-]+)\/?/
const MATCH_NOCOOKIE = /youtube-nocookie\.com/
const NOCOOKIE_HOST = 'https://www.youtube-nocookie.com'

function parsePlaylist (url) {
if (MATCH_PLAYLIST.test(url)) {
const [, playlistId] = url.match(MATCH_PLAYLIST)
return {
listType: 'playlist',
list: playlistId
}
} else if (MATCH_USER_UPLOADS.test(url)) {
const [, username] = url.match(MATCH_USER_UPLOADS)
return {
listType: 'user_uploads',
list: username
}
}
return {}
}

export default class YouTube extends Component {
static displayName = 'YouTube'
callPlayer = callPlayer
Expand All @@ -36,13 +19,20 @@ export default class YouTube extends Component {
this.props.onMount && this.props.onMount(this)
}

getID (url) {
if (!url || url instanceof Array) {
return null
}
return url.match(MATCH_URL_YOUTUBE)[1]
}

load (url, isReady) {
const { playing, muted, playsinline, controls, loop, config, onError } = this.props
const { playerVars, embedOptions } = config
const id = url && url.match(MATCH_URL_YOUTUBE)[1]
const id = this.getID(url)
if (isReady) {
if (MATCH_PLAYLIST.test(url) || MATCH_USER_UPLOADS.test(url)) {
this.player.loadPlaylist(parsePlaylist(url))
if (MATCH_PLAYLIST.test(url) || MATCH_USER_UPLOADS.test(url) || url instanceof Array) {
this.player.loadPlaylist(this.parsePlaylist(url))
return
}
this.player.cueVideoById({
Expand All @@ -66,7 +56,7 @@ export default class YouTube extends Component {
end: parseEndTime(url),
origin: window.location.origin,
playsinline: playsinline,
...parsePlaylist(url),
...this.parsePlaylist(url),
...playerVars
},
events: {
Expand All @@ -85,6 +75,29 @@ export default class YouTube extends Component {
}, onError)
}

parsePlaylist = (url) => {
if (url instanceof Array) {
return {
listType: 'playlist',
playlist: url.map(this.getID).join(',')
}
}
if (MATCH_PLAYLIST.test(url)) {
const [, playlistId] = url.match(MATCH_PLAYLIST)
return {
listType: 'playlist',
list: playlistId
}
} else if (MATCH_USER_UPLOADS.test(url)) {
const [, username] = url.match(MATCH_USER_UPLOADS)
return {
listType: 'user_uploads',
list: username
}
}
return {}
}

onStateChange = (event) => {
const { data } = event
const { onPlay, onPause, onBuffer, onBufferEnd, onEnded, onReady, loop, config: { playerVars, onUnstarted } } = this.props
Expand Down
7 changes: 6 additions & 1 deletion src/players/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ const canPlayFile = url => {
export default [
{
key: 'youtube',
canPlay: url => MATCH_URL_YOUTUBE.test(url),
canPlay: url => {
if (url instanceof Array) {
return url.every(item => MATCH_URL_YOUTUBE.test(item))
}
return MATCH_URL_YOUTUBE.test(url)
},
lazyPlayer: lazy(() => import(/* webpackChunkName: 'reactPlayerYouTube' */'./YouTube'))
},
{
Expand Down
3 changes: 3 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const MATCH_NUMERIC = /^\d+$/
// Parse YouTube URL for a start time param, ie ?t=1h14m30s
// and return the start time in seconds
function parseTimeParam (url, pattern) {
if (url instanceof Array) {
return undefined
}
const match = url.match(pattern)
if (match) {
const stamp = match[1]
Expand Down

0 comments on commit 4f8cfaa

Please sign in to comment.