Skip to content

Commit

Permalink
Add Facebook player
Browse files Browse the repository at this point in the history
  • Loading branch information
philip-luther committed Nov 22, 2024
1 parent 32cdd36 commit 11058c4
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Prop | Description
`youtubeConfig` | Configuration object for the YouTube player.<br />Set `playerVars` to override the [default player vars](https://developers.google.com/youtube/player_parameters?playerVersion=HTML5).<br />Set `preload` for [preloading](#preloading).
`vidmeConfig` | Configuration object for the Vidme player.<br />Set `format` to use a certain quality of video, when available.<br />Possible values: `240p`, `480p`, `720p`, `1080p`, `dash`, `hls`
`fileConfig` | Configuration object for the file player.<br />Set `attributes` to apply [element attributes](https://developer.mozilla.org/en/docs/Web/HTML/Element/video#Attributes).
`facebookConfig` | Configuration object for the Facebook player.<br />Set `appId` to your own [Facebook app ID](https://developers.facebook.com/docs/apps/register#app-id).

##### Preloading

Expand Down
3 changes: 3 additions & 0 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { propTypes, defaultProps } from './props'
import YouTube from './players/YouTube'
import SoundCloud from './players/SoundCloud'
import Vimeo from './players/Vimeo'
import Facebook from './players/Facebook'
import FilePlayer from './players/FilePlayer'
import Streamable from './players/Streamable'
import Vidme from './players/Vidme'
Expand Down Expand Up @@ -85,6 +86,8 @@ export default class ReactPlayer extends Component {
players.push(SoundCloud)
} else if (Vimeo.canPlay(url)) {
players.push(Vimeo)
} else if (Facebook.canPlay(url)) {
players.push(Facebook)
} else if (Streamable.canPlay(url)) {
players.push(Streamable)
} else if (Vidme.canPlay(url)) {
Expand Down
7 changes: 7 additions & 0 deletions src/demo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ export default class App extends Component {
{this.renderLoadButton('https://soundcloud.com/tycho/tycho-awake', 'Test B')}
</td>
</tr>
<tr>
<th>Facebook</th>
<td>
{this.renderLoadButton('https://www.facebook.com/facebook/videos/10153231379946729/', 'Test A')}
{this.renderLoadButton('https://www.facebook.com/FacebookDevelopers/videos/10152454700553553/', 'Test B')}
</td>
</tr>
<tr>
<th>Vimeo</th>
<td>
Expand Down
120 changes: 120 additions & 0 deletions src/players/Facebook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import React from 'react'
import loadScript from 'load-script'

import Base from './Base'

const SDK_URL = '//connect.facebook.net/en_US/sdk.js'
const SDK_GLOBAL = 'FB'
const SDK_GLOBAL_READY = 'fbAsyncInit'
const MATCH_URL = /^https:\/\/www\.facebook\.com\/([^/?].+\/)?video(s|\.php)[/?].*$/
const PLAYER_ID_PREFIX = 'facebook-player-'

export default class YouTube extends Base {
static displayName = 'Facebook'
static canPlay (url) {
return MATCH_URL.test(url)
}
playerID = PLAYER_ID_PREFIX + randomString()
getSDK () {
if (window[SDK_GLOBAL]) {
return Promise.resolve(window[SDK_GLOBAL])
}
return new Promise((resolve, reject) => {
const previousOnReady = window[SDK_GLOBAL_READY]
window[SDK_GLOBAL_READY] = function () {
if (previousOnReady) previousOnReady()
resolve(window[SDK_GLOBAL])
}
loadScript(SDK_URL, err => {
if (err) reject(err)
})
})
}
load (url) {
if (this.isReady) {
this.getSDK().then(FB => FB.XFBML.parse())
return
}
this.getSDK().then(FB => {
FB.init({
appId: this.props.facebookConfig.appId,
xfbml: true,
version: 'v2.5'
})
FB.Event.subscribe('xfbml.ready', msg => {
if (msg.type === 'video' && msg.id === this.playerID) {
this.player = msg.instance
this.player.subscribe('startedPlaying', this.onPlay)
this.player.subscribe('paused', this.props.onPause)
this.player.subscribe('finishedPlaying', this.onEnded)
this.player.subscribe('startedBuffering', this.props.onBuffer)
this.player.subscribe('error', this.props.onError)
this.onReady()
}
})
})
}
onEnded = () => {
const { loop, onEnded } = this.props
if (loop) {
this.seekTo(0)
}
onEnded()
}
play () {
if (!this.isReady) return
this.player.play()
}
pause () {
if (!this.isReady) return
this.player.pause()
}
stop () {
// No need to stop
}
seekTo (fraction) {
super.seekTo(fraction)
if (!this.isReady) return
this.player.seek(this.getDuration() * fraction)
}
setVolume (fraction) {
if (!this.isReady) return
this.player.setVolume(fraction)
}
setPlaybackRate () {
return null
}
getDuration () {
if (!this.isReady) return null
return this.player.getDuration()
}
getFractionPlayed () {
if (!this.isReady || !this.getDuration()) return null
return this.player.getCurrentPosition() / this.getDuration()
}
getFractionLoaded () {
return null
}
render () {
const style = {
width: '100%',
height: '100%',
backgroundColor: 'black'
}
return (
<div
style={style}
id={this.playerID}
className='fb-video'
data-href={this.props.url}
data-allowfullscreen='true'
data-controls={!this.props.controls ? 'false' : undefined}
/>
)
}
}

// http://stackoverflow.com/a/38622545
function randomString () {
return Math.random().toString(36).substr(2, 5)
}
6 changes: 6 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export const propTypes = {
playerVars: object,
preload: bool
}),
facebookConfig: shape({
appId: string
}),
vimeoConfig: shape({
iframeParams: object,
preload: bool
Expand Down Expand Up @@ -62,6 +65,9 @@ export const defaultProps = {
playerVars: {},
preload: false
},
facebookConfig: {
appId: '1309697205772819'
},
vimeoConfig: {
iframeParams: {},
preload: false
Expand Down

0 comments on commit 11058c4

Please sign in to comment.