Skip to content

Commit

Permalink
Tidy up Wistia player
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Dec 26, 2016
1 parent bede619 commit 9c82bfd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ReactPlayer
[![Dependency Status](https://img.shields.io/david/CookPete/react-player.svg)](https://david-dm.org/CookPete/react-player)
[![devDependency Status](https://img.shields.io/david/dev/CookPete/react-player.svg)](https://david-dm.org/CookPete/react-player?type=dev)

A react component for playing media from YouTube, SoundCloud, Streamable, Vidme, Vimeo, or Wistia as well as supported media files. Used by [rplayr](http://rplayr.com), an app to generate playlists from Reddit URLs.
A react component for playing a variety of URLs, including file paths, YouTube, SoundCloud, Streamable, Vidme, Vimeo and Wistia. Used by [rplayr](http://rplayr.com), an app to generate playlists from Reddit URLs.

The component parses a URL and loads in the appropriate markup and external SDKs to play media from [various sources](#supported-media). [Props](#props) can be passed in to control playback and react to events such as buffering or media ending.

Expand Down
55 changes: 23 additions & 32 deletions src/players/Wistia.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,36 @@ import loadScript from 'load-script'
import Base from './Base'

const SDK_URL = '//fast.wistia.com/assets/external/E-v1.js'
const SDK_GLOBAL = 'Wistia'
const MATCH_URL = /^https?:\/\/(.+)?(wistia.com|wi.st)\/(medias|embed)\/(.*)$/

export default class Wistia extends Base {
static displayName = 'Wistia'
static canPlay (url) {
return MATCH_URL.test(url)
}
constructor (props) {
super(props)
this.loadingSDK = true
this.player = null
}
componentDidMount () {
const { onStart, onPause, onEnded } = this.props
this.getSDK().then((_script) => {
this.loadingSDK = true

This comment has been minimized.

Copy link
@edelgado

edelgado Dec 26, 2016

Contributor

Makes sense, we aren't really loading the SDK in the constructor yet, we do it within the componentDidMount block.

this.getSDK().then(() => {
window._wq = window._wq || []
window._wq.push(
{
id: this.getVideoId(this.props.url),
onReady: (video) => {
this.loadingSDK = false
this.player = video
this.player.bind('start', onStart)
this.player.bind('play', this.onPlay)
this.player.bind('pause', onPause)
this.player.bind('end', onEnded)
this.onReady()
}
window._wq.push({
id: this.getID(this.props.url),
onReady: player => {
this.loadingSDK = false
this.player = player
this.player.bind('start', onStart)
this.player.bind('play', this.onPlay)
this.player.bind('pause', onPause)
this.player.bind('end', onEnded)
this.onReady()
}
)
})
})
}
getSDK () {
return new Promise((resolve, reject) => {
if (window.Wistia) {
if (window[SDK_GLOBAL]) {
resolve()
} else {
loadScript(SDK_URL, (err, script) => {
Expand All @@ -48,13 +43,15 @@ export default class Wistia extends Base {
}
})
}
getID (url) {
return url && url.match(MATCH_URL)[4]
}
load (url) {
const wistiaId = this.getVideoId(url)
const id = this.getID(url)
if (this.isReady) {
this.player.replaceWith(wistiaId)
this.player.replaceWith(id)
this.props.onReady()
this.onReady()
return
}
}
play () {
Expand Down Expand Up @@ -93,22 +90,16 @@ export default class Wistia extends Base {
getFractionLoaded () {
return null
}
getVideoId (url) {
return url && url.match(MATCH_URL)[4]
}
ref = container => {
this.container = container
}
render () {
const id = this.getID(this.props.url)
const className = `wistia_embed wistia_async_${id}`

This comment has been minimized.

Copy link
@edelgado

edelgado Dec 26, 2016

Contributor

Nice

const style = {
width: '100%',
height: '100%',
display: this.props.url ? 'block' : 'none'
}

const id = this.getVideoId(this.props.url)
return (
<div ref={this.ref} className={`wistia_embed wistia_async_${id}`} style={style} />
<div className={className} style={style} />
)
}
}

0 comments on commit 9c82bfd

Please sign in to comment.