Skip to content

Commit

Permalink
Add support for custom players (cookpete#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrooklynKing authored and cookpete committed Apr 5, 2018
1 parent c9438ae commit b97b19f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ If you aren’t using React, you can still render a player using the standalone

See [`jsFiddle` example](https://jsfiddle.net/krkcvx9s/)

#### Adding your own players

If you have your own player, that is compatible with ReactPlayer's internal architecture, you can use it like this:

```javascript
import YourOwnPlayer from './somewhere';
ReactPlayer.addCustomPlayer(YourOwnPlayer);
```

Or you can clear all additional players:

```javascript
ReactPlayer.removeCustomPlayers();
```

It is your responsibility to ensure that custom players keep up with any internal changes to ReactPlayer in later versions.

#### Using Bower

```bash
Expand Down
12 changes: 10 additions & 2 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import renderPreloadPlayers from './preload'

const SUPPORTED_PROPS = Object.keys(propTypes)

let customPlayers = []

export default class ReactPlayer extends Component {
static addCustomPlayer = player => {
customPlayers.push(player)
}
static removeCustomPlayers = () => {
customPlayers = []
}
static displayName = 'ReactPlayer'
static propTypes = propTypes
static defaultProps = defaultProps
static canPlay = url => {
for (let Player of players) {
for (let Player of [ ...customPlayers, ...players ]) {
if (Player.canPlay(url)) {
return true
}
Expand Down Expand Up @@ -51,7 +59,7 @@ export default class ReactPlayer extends Component {
this.player.seekTo(fraction)
}
getActivePlayer (url) {
for (let Player of players) {
for (let Player of [ ...customPlayers, ...players ]) {
if (Player.canPlay(url)) {
return Player
}
Expand Down
30 changes: 30 additions & 0 deletions test/specs/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'

import ReactPlayer from '../../src/ReactPlayer'
import { FilePlayer } from '../../src/players/FilePlayer'

const { describe, it, expect, beforeEach, afterEach } = window

Expand Down Expand Up @@ -457,4 +458,33 @@ describe('ReactPlayer', () => {
expect(el.querySelectorAll('video').length).to.equal(1)
})
})

describe('custom players', () => {
class CustomPlayer extends React.Component {
static canPlay = url => true
static displayName = 'CustomPlayer'
load () {}
render () {
return (
<div />
)
}
}

it('could be added with usage of static method', () => {
ReactPlayer.addCustomPlayer(CustomPlayer)
renderPlayer({
url: 'test:url'
})
expect(player.player.player instanceof CustomPlayer).to.be.true
})

it('could be cleared with usage of static method', () => {
ReactPlayer.removeCustomPlayers()
renderPlayer({
url: 'test:url'
})
expect(player.player.player instanceof FilePlayer).to.be.true
})
})
})

0 comments on commit b97b19f

Please sign in to comment.