Skip to content

Commit 66482d5

Browse files
committed
Add FilePlayer for HTML5 media files
1 parent f9f9730 commit 66482d5

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

src/App.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export default class App extends Component {
5858
<button onClick={this.load.bind(this, 'https://www.youtube.com/watch?v=oUFJJNQGwhk')}>Youtube video</button>
5959
<button onClick={this.load.bind(this, 'https://soundcloud.com/miami-nights-1984/accelerated')}>Soundcloud song</button>
6060
<button onClick={this.load.bind(this, 'https://vimeo.com/90509568')}>Vimeo video</button>
61+
<button onClick={this.load.bind(this, 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4')}>MP4 video</button>
62+
<button onClick={this.load.bind(this, 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv')}>OGV video</button>
63+
<button onClick={this.load.bind(this, 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.webm')}>WEBM video</button>
6164
<input
6265
type='range' min={0} max={1} step='any'
6366
value={this.state.played}

src/players/FilePlayer.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react'
2+
3+
import propTypes from '../propTypes'
4+
import Base from './Base'
5+
6+
const VIDEO_EXTENSIONS = /\.(mp4|og[gv]|webm)$/
7+
const AUDIO_EXTENSIONS = /\.(mp3|wav)$/
8+
9+
export default class FilePlayer extends Base {
10+
static propTypes = propTypes
11+
static canPlay (url) {
12+
return VIDEO_EXTENSIONS.test(url) || AUDIO_EXTENSIONS.test(url)
13+
}
14+
componentDidMount () {
15+
this.player = React.findDOMNode(this.refs.player)
16+
this.player.onended = this.props.onEnded
17+
this.player.onplay = this.props.onPlay
18+
this.player.onpause = this.props.onPause
19+
super.componentDidMount()
20+
}
21+
shouldComponentUpdate (nextProps) {
22+
return this.props.url !== nextProps
23+
}
24+
play (url) {
25+
this.player.play()
26+
}
27+
pause () {
28+
this.player.pause()
29+
}
30+
stop () {
31+
// No need to stop
32+
}
33+
seekTo (fraction) {
34+
this.player.currentTime = this.player.duration * fraction
35+
}
36+
setVolume (fraction) {
37+
this.player.volume = fraction
38+
}
39+
getFractionPlayed () {
40+
if (this.player.readyState === 0) return 0
41+
return this.player.currentTime / this.player.duration
42+
}
43+
getFractionLoaded () {
44+
if (this.player.readyState === 0) return 0
45+
return this.player.buffered.end(0) / this.player.duration
46+
}
47+
render () {
48+
const Media = AUDIO_EXTENSIONS.test(this.props.url) ? 'audio' : 'video'
49+
return (
50+
<Media
51+
ref='player'
52+
src={this.props.url}
53+
width={this.props.width}
54+
height={this.props.height}
55+
autoPlay
56+
/>
57+
)
58+
}
59+
}

src/players/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import YouTube from './YouTube'
22
import SoundCloud from './SoundCloud'
33
import Vimeo from './Vimeo'
4+
import FilePlayer from './FilePlayer'
45

5-
export default [ YouTube, SoundCloud, Vimeo ]
6+
export default [ YouTube, SoundCloud, Vimeo, FilePlayer ]

test/canPlay.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { expect } from 'chai'
44
import SoundCloud from '../src/players/SoundCloud'
55
import YouTube from '../src/players/YouTube'
66
import Vimeo from '../src/players/Vimeo'
7+
import FilePlayer from '../src/players/FilePlayer'
78

89
describe('YouTube', () => {
910
it('knows what it can play', () => {
@@ -43,3 +44,21 @@ describe('Vimeo', () => {
4344
expect(Vimeo.canPlay('https://www.youtube.com/watch?v=1234')).to.be.false
4445
})
4546
})
47+
48+
describe('FilePlayer', () => {
49+
it('knows what it can play', () => {
50+
expect(FilePlayer.canPlay('http://example.com/file.mp4')).to.be.true
51+
expect(FilePlayer.canPlay('http://example.com/file.ogg')).to.be.true
52+
expect(FilePlayer.canPlay('http://example.com/file.ogv')).to.be.true
53+
expect(FilePlayer.canPlay('http://example.com/file.webm')).to.be.true
54+
expect(FilePlayer.canPlay('http://example.com/file.mp3')).to.be.true
55+
expect(FilePlayer.canPlay('http://example.com/file.wav')).to.be.true
56+
})
57+
58+
it('knows what it can\'t play', () => {
59+
expect(FilePlayer.canPlay('http://example.com/file.mp5')).to.be.false
60+
expect(FilePlayer.canPlay('http://example.com/file.ogh')).to.be.false
61+
expect(FilePlayer.canPlay('http://example.com/file.web')).to.be.false
62+
expect(FilePlayer.canPlay('http://example.com/file.txt')).to.be.false
63+
})
64+
})

0 commit comments

Comments
 (0)