Skip to content

Commit

Permalink
Bring back static canPlay util
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Sep 9, 2017
1 parent 44a8acc commit f4861d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,17 @@ You can also specify a `type` for each source by using objects with `src` and `t

### Methods

Use [`ref`](https://facebook.github.io/react/docs/refs-and-the-dom.html) to call methods on the player. See [the demo app](src/demo/App.js) for an example of this.
#### Static Methods

Prop | Description
Method | Description
---- | -----------
`ReactPlayer.canPlay(url)` | Determine if a URL can be played. This does *not* detect media that is unplayable due to privacy settings, streaming permissions, etc. In that case, the `onError` prop will be invoked after attemping to play. Any URL that does not match any patterns will fall back to a native HTML5 media player.

#### Instance Methods

Use [`ref`](https://facebook.github.io/react/docs/refs-and-the-dom.html) to call instance methods on the player. See [the demo app](src/demo/App.js) for an example of this.

Method | Description
---- | -----------
`seekTo(amount)` | Seek to the given number of seconds, or fraction if `amount` is between `0` and `1`.
`getCurrentTime()` | Returns the number of seconds that has been played.<br >Returns `null` if duration is unavailable.
Expand Down
9 changes: 9 additions & 0 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export default class ReactPlayer extends Component {
static displayName = 'ReactPlayer'
static propTypes = propTypes
static defaultProps = defaultProps
static canPlay = url => {
const players = [...SUPPORTED_PLAYERS, FilePlayer]
for (let Player of players) {
if (Player.canPlay(url)) {
return true
}
}
return false
}
config = getConfig(this.props, defaultProps, true)
componentDidMount () {
this.progress()
Expand Down
8 changes: 7 additions & 1 deletion src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Base from './Base'
import { getSDK } from '../utils'

const AUDIO_EXTENSIONS = /\.(m4a|mp4a|mpga|mp2|mp2a|mp3|m2a|m3a|wav|weba|aac|oga|spx)($|\?)/i
const VIDEO_EXTENSIONS = /\.(mp4|og[gv]|webm|mov|m4v)($|\?)/i
const HLS_EXTENSIONS = /\.(m3u8)($|\?)/i
const HLS_SDK_URL = 'https://cdn.jsdelivr.net/hls.js/latest/hls.min.js'
const HLS_GLOBAL = 'Hls'
Expand All @@ -14,7 +15,12 @@ const DASH_GLOBAL = 'dashjs'
export default class FilePlayer extends Base {
static displayName = 'FilePlayer'
static canPlay (url) {
return true
return (
AUDIO_EXTENSIONS.test(url) ||
VIDEO_EXTENSIONS.test(url) ||
HLS_EXTENSIONS.test(url) ||
DASH_EXTENSIONS.test(url)
)
}
componentDidMount () {
const { playsinline, onPause, onEnded, onError } = this.props
Expand Down

0 comments on commit f4861d7

Please sign in to comment.