Skip to content

Commit f1395d1

Browse files
committed
pass configuration options to players via props
1 parent 4eab6bd commit f1395d1

7 files changed

Lines changed: 68 additions & 10 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ playing | Set to `true` or `false` to pause or play the media
4848
volume | Sets the volume of the appropriate player
4949
width | Sets the width of the player
5050
height | Sets the height of the player
51+
soundcloudConfig | An object containing configuration for the SoundCloud player. Includes `clientId`, which can be used to override the default `client_id`
52+
vimeoConfig | An object containing configuration for the Vimeo player. Includes `iframeParams`, which maps to the [parameters accepted by the Vimeo iframe player](https://developer.vimeo.com/player/embedding#universal-parameters)
53+
youtubeConfig | An object containing configuration for the YouTube player. Includes `playerVars`, which maps to the [parameters accepted by the YouTube iframe player](https://developers.google.com/youtube/player_parameters?playerVersion=HTML5)
5154
onProgress | Callback containing `played` and `loaded` progress as a fraction eg `{ played: 0.12, loaded: 0.34 }`
5255
onPlay | Called when media starts or resumes playing after pausing or buffering
5356
onPause | Called when media is paused

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
},
5454
"dependencies": {
5555
"array.prototype.find": "^1.0.0",
56-
"load-script": "^1.0.0"
56+
"load-script": "^1.0.0",
57+
"query-string": "^3.0.0"
5758
},
5859
"standard": {
5960
"parser": "babel-eslint",

src/App.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export default class App extends Component {
3838
this.setState(state)
3939
}
4040
}
41+
onConfigSubmit = () => {
42+
let config
43+
try {
44+
config = JSON.parse(this.refs.config.value)
45+
} catch (error) {
46+
config = {}
47+
console.error('Error setting config:', error)
48+
}
49+
this.setState(config)
50+
}
4151
render () {
4252
return (
4353
<div>
@@ -48,6 +58,9 @@ export default class App extends Component {
4858
playing={this.state.playing}
4959
volume={this.state.volume}
5060
onProgress={this.onProgress}
61+
soundcloudConfig={this.state.soundcloudConfig}
62+
vimeoConfig={this.state.vimeoConfig}
63+
youtubeConfig={this.state.youtubeConfig}
5164
onPlay={() => console.log('onPlay')}
5265
onPause={() => console.log('onPause')}
5366
onBuffer={() => console.log('onBuffer')}
@@ -61,20 +74,26 @@ export default class App extends Component {
6174
<button onClick={this.load.bind(this, 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4')}>MP4 video</button>
6275
<button onClick={this.load.bind(this, 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv')}>OGV video</button>
6376
<button onClick={this.load.bind(this, 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.webm')}>WEBM video</button>
64-
<input
77+
<input ref='url' placeholder='url' />
78+
<button onClick={() => { this.load(this.refs.url.value) }}>Load URL</button>
79+
<hr />
80+
seek: <input
6581
type='range' min={0} max={1} step='any'
6682
value={this.state.played}
6783
onMouseDown={this.onSeekMouseDown}
6884
onChange={this.onSeekChange}
6985
onMouseUp={this.onSeekMouseUp}
7086
/>
71-
<input
87+
played: <progress max={1} value={this.state.played} />
88+
loaded: <progress max={1} value={this.state.loaded} />
89+
volume: <input
7290
type='range' min={0} max={1} step='any'
7391
value={this.state.volume}
7492
onChange={this.setVolume}
7593
/>
76-
played: <progress max={1} value={this.state.played} />
77-
loaded: <progress max={1} value={this.state.loaded} />
94+
<hr />
95+
<textarea ref='config' placeholder='Config JSON' style={{width: '200px', height: '200px'}}></textarea>
96+
<button onClick={this.onConfigSubmit}>Update Config</button>
7897
</div>
7998
)
8099
}

src/players/SoundCloud.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import loadScript from 'load-script'
44
import propTypes from '../propTypes'
55
import Base from './Base'
66

7-
const CLIENT_ID = 'e8b6f84fbcad14c301ca1355cae1dea2'
7+
const DEFAULT_CLIENT_ID = 'e8b6f84fbcad14c301ca1355cae1dea2'
88
const SDK_URL = '//connect.soundcloud.com/sdk-2.0.0.js'
99
const SDK_GLOBAL = 'SC'
1010
const RESOLVE_URL = '//api.soundcloud.com/resolve.json'
1111
const MATCH_URL = /^https?:\/\/(soundcloud.com|snd.sc)\/([a-z0-9-]+\/[a-z0-9-]+)$/
1212

1313
export default class SoundCloud extends Base {
1414
static propTypes = propTypes
15+
static defaultProps = {
16+
soundcloudConfig: {
17+
clientId: DEFAULT_CLIENT_ID
18+
}
19+
}
1520
static canPlay (url) {
1621
return MATCH_URL.test(url)
1722
}
@@ -30,14 +35,14 @@ export default class SoundCloud extends Base {
3035
if (err) {
3136
reject(err)
3237
} else {
33-
window[SDK_GLOBAL].initialize({ client_id: CLIENT_ID })
38+
window[SDK_GLOBAL].initialize({ client_id: this.props.soundcloudConfig.clientId })
3439
resolve(window[SDK_GLOBAL])
3540
}
3641
})
3742
})
3843
}
3944
getSongData (url) {
40-
return fetch(RESOLVE_URL + '?url=' + url + '&client_id=' + CLIENT_ID)
45+
return fetch(RESOLVE_URL + '?url=' + url + '&client_id=' + this.props.soundcloudConfig.clientId)
4146
.then(response => response.json())
4247
}
4348
play (url) {

src/players/Vimeo.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import React from 'react'
2+
import queryString from 'query-string'
23

34
import propTypes from '../propTypes'
45
import Base from './Base'
56

67
const IFRAME_SRC = 'https://player.vimeo.com/video/'
78
const MATCH_URL = /https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/
89
const MATCH_MESSAGE_ORIGIN = /^https?:\/\/player.vimeo.com/
10+
const DEFAULT_IFRAME_PARAMS = {
11+
api: 1,
12+
autoplay: 1,
13+
badge: 0,
14+
byline: 0,
15+
portrait: 0,
16+
title: 0
17+
}
918

1019
export default class Vimeo extends Base {
1120
static propTypes = propTypes
21+
static defaultProps = {
22+
vimeoConfig: {}
23+
}
1224
static canPlay (url) {
1325
return MATCH_URL.test(url)
1426
}
@@ -73,10 +85,11 @@ export default class Vimeo extends Base {
7385
width: '100%',
7486
height: '100%'
7587
}
88+
const iframeParams = { ...DEFAULT_IFRAME_PARAMS, ...this.props.vimeoConfig.iframeParams }
7689
return (
7790
<iframe
7891
ref='iframe'
79-
src={IFRAME_SRC + id + '?api=1&autoplay=1&badge=0&byline=0&portrait=0&title=0'}
92+
src={IFRAME_SRC + id + '?' + queryString.stringify(iframeParams)}
8093
style={style}
8194
frameBorder='0'
8295
/>

src/players/YouTube.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ const SDK_URL = '//www.youtube.com/iframe_api'
88
const SDK_GLOBAL = 'YT'
99
const MATCH_URL = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/
1010
const PLAYER_ID = 'youtube-player'
11+
const DEFAULT_PLAYER_VARS = {
12+
autoplay: 1,
13+
controls: 0,
14+
showinfo: 0
15+
}
1116

1217
export default class YouTube extends Base {
1318
static propTypes = propTypes
19+
static defaultProps = {
20+
youtubeConfig: {}
21+
}
1422
static canPlay (url) {
1523
return MATCH_URL.test(url)
1624
}
@@ -45,7 +53,7 @@ export default class YouTube extends Base {
4553
width: '100%',
4654
height: '100%',
4755
videoId: id,
48-
playerVars: { autoplay: 1, controls: 0, showinfo: 0 },
56+
playerVars: { ...DEFAULT_PLAYER_VARS, ...this.props.youtubeConfig.playerVars },
4957
events: {
5058
onStateChange: this.onStateChange,
5159
onError: this.props.onError

src/propTypes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ export default {
66
volume: PropTypes.number,
77
width: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
88
height: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
9+
soundcloudConfig: PropTypes.shape({
10+
clientId: PropTypes.string
11+
}),
12+
youtubeConfig: PropTypes.shape({
13+
playerVars: PropTypes.object
14+
}),
15+
vimeoConfig: PropTypes.shape({
16+
iframeParams: PropTypes.object
17+
}),
918
onPlay: PropTypes.func,
1019
onPause: PropTypes.func,
1120
onBuffer: PropTypes.func,

0 commit comments

Comments
 (0)