Skip to content

Commit

Permalink
When seeking before player is ready, store value and seek later
Browse files Browse the repository at this point in the history
  • Loading branch information
happydev829 committed Jan 4, 2016
1 parent 156b076 commit 4f10cdf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
20 changes: 18 additions & 2 deletions src/players/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Component } from 'react'

import { propTypes, defaultProps } from '../props'

const SEEK_ON_READY_EXPIRY = 5000

export default class Base extends Component {
static propTypes = propTypes
static defaultProps = defaultProps
Expand All @@ -17,7 +19,7 @@ export default class Base extends Component {
// Invoke player methods based on incoming props
if (this.props.url !== nextProps.url && nextProps.url) {
this.load(nextProps.url, nextProps.playing)
this.props.onProgress({ played: 0, loaded: 0 }) // Needed?
this.seekOnReady = null
} else if (this.props.url && !nextProps.url) {
this.stop()
clearTimeout(this.updateTimeout)
Expand All @@ -33,9 +35,23 @@ export default class Base extends Component {
return this.props.url !== nextProps.url
}
isReady = false
seekTo (fraction) {
// When seeking before player is ready, store value and seek later
if (!this.isReady && fraction !== 0) {
this.seekOnReady = fraction
setTimeout(() => this.seekOnReady = null, SEEK_ON_READY_EXPIRY)
}
}
onPlay = () => {
this.props.onPlay()
this.setVolume(this.props.volume)
if (this.seekOnReady) {
this.seekTo(this.seekOnReady)
this.seekOnReady = null
}
}
onReady = () => {
this.isReady = true
this.setVolume(this.props.volume)
if (this.props.playing || this.preloading) {
this.preloading = false
if (this.loadOnReady) {
Expand Down
3 changes: 2 additions & 1 deletion src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class FilePlayer extends Base {
componentDidMount () {
this.player = this.refs.player
this.player.oncanplay = this.onReady
this.player.onplay = this.props.onPlay
this.player.onplay = this.onPlay
this.player.onpause = this.props.onPause
this.player.onended = this.props.onEnded
this.player.onerror = this.props.onError
Expand All @@ -31,6 +31,7 @@ export default class FilePlayer extends Base {
this.player.src = ''
}
seekTo (fraction) {
super.seekTo(fraction)
this.player.currentTime = this.player.duration * fraction
}
setVolume (fraction) {
Expand Down
3 changes: 2 additions & 1 deletion src/players/SoundCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class SoundCloud extends Base {
})
}
onStateChange = state => {
if (state === 'playing') this.props.onPlay()
if (state === 'playing') this.onPlay()
if (state === 'paused') this.props.onPause()
if (state === 'loading') this.props.onBuffer()
if (state === 'ended') this.props.onEnded()
Expand All @@ -86,6 +86,7 @@ export default class SoundCloud extends Base {
this.player.stop()
}
seekTo (fraction) {
super.seekTo(fraction)
if (!this.isReady) return
this.player.seek(this.player.getDuration() * fraction)
}
Expand Down
3 changes: 2 additions & 1 deletion src/players/Vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class Vimeo extends Base {
this.iframe.src = ''
}
seekTo (fraction) {
super.seekTo(fraction)
this.postMessage('seekTo', this.duration * fraction)
}
setVolume (fraction) {
Expand All @@ -76,7 +77,7 @@ export default class Vimeo extends Base {
if (data.event === 'ready') this.onReady()
if (data.event === 'playProgress') this.fractionPlayed = data.data.percent
if (data.event === 'loadProgress') this.fractionLoaded = data.data.percent
if (data.event === 'play') this.props.onPlay()
if (data.event === 'play') this.onPlay()
if (data.event === 'pause') this.props.onPause()
if (data.event === 'finish') this.props.onEnded()
if (data.method === 'getDuration') this.duration = data.value // Store for use in seekTo()
Expand Down
4 changes: 2 additions & 2 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default class YouTube extends Base {
} else {
this.player.cueVideoById(id)
}
this.setVolume(this.props.volume)
return
}
if (this.loadingSDK) {
Expand Down Expand Up @@ -84,7 +83,7 @@ export default class YouTube extends Base {
}
onStateChange = ({ data }) => {
const { PLAYING, PAUSED, BUFFERING, ENDED } = window[SDK_GLOBAL].PlayerState
if (data === PLAYING) this.props.onPlay()
if (data === PLAYING) this.onPlay()
if (data === PAUSED) this.props.onPause()
if (data === BUFFERING) this.props.onBuffer()
if (data === ENDED) this.props.onEnded()
Expand All @@ -102,6 +101,7 @@ export default class YouTube extends Base {
this.player.stopVideo()
}
seekTo (fraction) {
super.seekTo(fraction)
if (!this.isReady || !this.player.seekTo) return
this.player.seekTo(this.player.getDuration() * fraction)
}
Expand Down

0 comments on commit 4f10cdf

Please sign in to comment.