-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React from 'react' | ||
import loadScript from 'load-script' | ||
|
||
import Base from './Base' | ||
import { randomString } from '../utils' | ||
|
||
const SDK_URL = '//player.twitch.tv/js/embed/v1.js' | ||
const SDK_GLOBAL = 'Twitch' | ||
const MATCH_VIDEO_URL = /^(?:https?:\/\/)?(?:www\.)twitch\.tv\/videos\/(\d+)($|\?)/ | ||
const MATCH_CHANNEL_URL = /^(?:https?:\/\/)?(?:www\.)twitch\.tv\/([a-z0-9_]+)($|\?)/ | ||
const PLAYER_ID_PREFIX = 'twitch-player-' | ||
|
||
export default class YouTube extends Base { | ||
static displayName = 'Twitch' | ||
static canPlay (url) { | ||
return MATCH_VIDEO_URL.test(url) || MATCH_CHANNEL_URL.test(url) | ||
} | ||
playerID = PLAYER_ID_PREFIX + randomString() | ||
getSDK () { | ||
if (window[SDK_GLOBAL]) { | ||
return Promise.resolve(window[SDK_GLOBAL]) | ||
} | ||
return new Promise((resolve, reject) => { | ||
loadScript(SDK_URL, err => { | ||
if (err) reject(err) | ||
resolve(window[SDK_GLOBAL]) | ||
}) | ||
}) | ||
} | ||
load (url) { | ||
const { playsinline, onError } = this.props | ||
const isChannel = MATCH_CHANNEL_URL.test(url) | ||
const id = isChannel ? url.match(MATCH_CHANNEL_URL)[1] : url.match(MATCH_VIDEO_URL)[1] | ||
if (this.isReady) { | ||
if (isChannel) { | ||
this.player.setChannel(id) | ||
} else { | ||
this.player.setVideo('v' + id) | ||
} | ||
return | ||
} | ||
if (this.loadingSDK) { | ||
this.loadOnReady = url | ||
return | ||
} | ||
this.loadingSDK = true | ||
this.getSDK().then(Twitch => { | ||
this.player = new Twitch.Player(this.playerID, { | ||
video: isChannel ? '' : id, | ||
channel: isChannel ? id : '', | ||
height: '100%', | ||
width: '100%', | ||
playsinline: playsinline | ||
}) | ||
const { READY, PLAY, PAUSE, ENDED } = Twitch.Player | ||
this.player.addEventListener(READY, this.onReady) | ||
this.player.addEventListener(PLAY, this.onPlay) | ||
this.player.addEventListener(PAUSE, this.props.onPause) | ||
this.player.addEventListener(ENDED, this.onEnded) | ||
}, onError) | ||
} | ||
onEnded = () => { | ||
const { loop, onEnded } = this.props | ||
if (loop) { | ||
this.seekTo(0) | ||
} | ||
onEnded() | ||
} | ||
call (method, ...args) { | ||
if (!this.isReady || !this.player || !this.player[method]) return | ||
return this.player[method](...args) | ||
} | ||
play () { | ||
this.call('play') | ||
} | ||
pause () { | ||
this.call('pause') | ||
} | ||
stop () { | ||
this.call('pause') | ||
} | ||
seekTo (amount) { | ||
const seconds = super.seekTo(amount) | ||
this.call('seek', seconds) | ||
} | ||
setVolume (fraction) { | ||
this.call('setVolume', fraction) | ||
} | ||
setPlaybackRate (rate) { | ||
return null | ||
} | ||
getDuration () { | ||
return this.call('getDuration') | ||
} | ||
getFractionPlayed () { | ||
const time = this.call('getCurrentTime') | ||
const duration = this.getDuration() | ||
if (time && duration) { | ||
return time / duration | ||
} | ||
return null | ||
} | ||
getFractionLoaded () { | ||
return null | ||
} | ||
render () { | ||
const style = { | ||
width: '100%', | ||
height: '100%' | ||
} | ||
return ( | ||
<div style={style} id={this.playerID} /> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters