-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FilePlayer for HTML5 media files
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React from 'react' | ||
|
||
import propTypes from '../propTypes' | ||
import Base from './Base' | ||
|
||
const VIDEO_EXTENSIONS = /\.(mp4|og[gv]|webm)$/ | ||
const AUDIO_EXTENSIONS = /\.(mp3|wav)$/ | ||
|
||
export default class FilePlayer extends Base { | ||
static propTypes = propTypes | ||
static canPlay (url) { | ||
return VIDEO_EXTENSIONS.test(url) || AUDIO_EXTENSIONS.test(url) | ||
} | ||
componentDidMount () { | ||
this.player = React.findDOMNode(this.refs.player) | ||
this.player.onended = this.props.onEnded | ||
this.player.onplay = this.props.onPlay | ||
this.player.onpause = this.props.onPause | ||
super.componentDidMount() | ||
} | ||
shouldComponentUpdate (nextProps) { | ||
return this.props.url !== nextProps | ||
} | ||
play (url) { | ||
this.player.play() | ||
} | ||
pause () { | ||
this.player.pause() | ||
} | ||
stop () { | ||
// No need to stop | ||
} | ||
seekTo (fraction) { | ||
this.player.currentTime = this.player.duration * fraction | ||
} | ||
setVolume (fraction) { | ||
this.player.volume = fraction | ||
} | ||
getFractionPlayed () { | ||
if (this.player.readyState === 0) return 0 | ||
return this.player.currentTime / this.player.duration | ||
} | ||
getFractionLoaded () { | ||
if (this.player.readyState === 0) return 0 | ||
return this.player.buffered.end(0) / this.player.duration | ||
} | ||
render () { | ||
const Media = AUDIO_EXTENSIONS.test(this.props.url) ? 'audio' : 'video' | ||
return ( | ||
<Media | ||
ref='player' | ||
src={this.props.url} | ||
width={this.props.width} | ||
height={this.props.height} | ||
autoPlay | ||
/> | ||
) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import YouTube from './YouTube' | ||
import SoundCloud from './SoundCloud' | ||
import Vimeo from './Vimeo' | ||
import FilePlayer from './FilePlayer' | ||
|
||
export default [ YouTube, SoundCloud, Vimeo ] | ||
export default [ YouTube, SoundCloud, Vimeo, FilePlayer ] |
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