Skip to content

Commit

Permalink
allow each vendor to handle looping
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Feb 21, 2017
1 parent 3d4805c commit 1fe7d17
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 25 deletions.
7 changes: 3 additions & 4 deletions example/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class MediaApp extends Component {
volume: 1,
fullscreen: false,
loop: false,
playbackRate: 0.5,
playbackRate: 1,
useAudioObject: false,
}

Expand All @@ -136,8 +136,8 @@ class MediaApp extends Component {
<div>
<Player
src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
// src="http://a1083.phobos.apple.com/us/r1000/014/Music/v4/4e/44/b7/4e44b7dc-aaa2-c63b-fb38-88e1635b5b29/mzaf_1844128138535731917.plus.aac.p.m4a"
// src="http://www.youtube.com/embed/h3YVKTxTOgU"
src="http://a1083.phobos.apple.com/us/r1000/014/Music/v4/4e/44/b7/4e44b7dc-aaa2-c63b-fb38-88e1635b5b29/mzaf_1844128138535731917.plus.aac.p.m4a"
src="http://www.youtube.com/embed/h3YVKTxTOgU"
// src="https://player.vimeo.com/video/156147818"
currentTime={currentTime}
playing={playing}
Expand All @@ -160,7 +160,6 @@ class MediaApp extends Component {
/>

<div>
<h3>Controls</h3>
<button onClick={this.playPause}>
{ playing ? 'Pause' : 'Play' }
</button>
Expand Down
30 changes: 13 additions & 17 deletions src/Player.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ class Player extends Component {
screenfull.raw.fullscreenchange,
this._handleFullscreenChange
)

if (this.props.currentTime !== 0) {
this.seekTo(this.props.currentTime)
}

if (this._player.setPlaybackRate) {
this._player.setPlaybackRate(this.props.playbackRate)
}
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -96,6 +88,10 @@ class Player extends Component {
this._player.setPlaybackRate(nextProps.playbackRate)
}

if (this.props.loop !== nextProps.loop) {
this._player.setLoop(nextProps.loop)
}

if (this.props.fullscreen !== nextProps.fullscreen) {
if (nextProps.fullscreen) {
screenfull.request(this._player.getNode())
Expand Down Expand Up @@ -132,6 +128,15 @@ class Player extends Component {
if (this.props.autoPlay) {
this._player.play()
}

if (this.props.currentTime !== 0) {
this.seekTo(this.props.currentTime)
}

if (this._player.setPlaybackRate) {
this._player.setPlaybackRate(this.props.playbackRate)
}

this.props.onReady(e)
}

Expand All @@ -145,14 +150,6 @@ class Player extends Component {
this.props.onVolumeChange(volume)
}

_handleEnded = (e) => {
if (this.props.loop) {
this._player.seekTo(0)
this._player.play()
}
this.props.onEnded(e)
}

render() {
const { src } = this.props
const { vendor, component } = getVendor(src, this.props.vendor)
Expand All @@ -167,7 +164,6 @@ class Player extends Component {
onReady: this._handleReady,
onTimeUpdate: this._handleTimeUpdate,
onVolumeChange: this._handleVolumeChange,
onEnded: this._handleEnded,
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/vendors/HTML5.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class HTML5 extends Component {
this._player.playbackRate = rate
}

setLoop(loop) {
this._player.loop = loop
}

getNode() {
return findDOMNode(this._player)
}
Expand Down Expand Up @@ -85,7 +89,6 @@ class HTML5 extends Component {
const props = specialAssign({
ref: c => this._player = c,
src: this.props.src,
loop: this.props.loop,
onCanPlay: this._handleCanPlay,
onPlay: this._handlePlay,
onPause: this._handlePause,
Expand Down
4 changes: 4 additions & 0 deletions src/vendors/Vimeo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class Vimeo extends Component {
this._player.setVolume(volume)
}

setLoop(loop) {
this._player.setLoop(loop)
}

getNode() {
return this._player.element
}
Expand Down
14 changes: 11 additions & 3 deletions src/vendors/Youtube.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Youtube extends Component {
this.props.onReady()
},
onStateChange: ({ data }) => {
const { PLAYING, PAUSED, ENDED, BUFFERING, CUED } = window.YT.PlayerState
const { PLAYING, PAUSED, ENDED, BUFFERING, CUED } = YT.PlayerState
const isPlaying = (data === PLAYING)

if (isPlaying) {
Expand All @@ -99,11 +99,15 @@ class Youtube extends Component {
}

if (data === PAUSED) {
this.props.onPause(false)
this.props.onPause()
}

if (data === ENDED) {
this.props.onEnded(false)
this.props.onEnded()

if (this._loop) {
this.play()
}
}

// start fetching progress when playing or buffering
Expand Down Expand Up @@ -155,6 +159,10 @@ class Youtube extends Component {
this._player.setPlaybackRate(rate)
}

setLoop(loop) {
this._loop = loop
}

_handleProgress = () => {
if (!this._isMounted) return

Expand Down
1 change: 1 addition & 0 deletions src/vendors/vendor-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PropTypes } from 'react'
export default {
vendor: PropTypes.string,
src: PropTypes.string,
loop: PropTypes.bool,
onReady: PropTypes.func,
onPlay: PropTypes.func,
onPause: PropTypes.func,
Expand Down

0 comments on commit 1fe7d17

Please sign in to comment.