Skip to content

Commit

Permalink
Use explicit isReady property to guard player methods
Browse files Browse the repository at this point in the history
  • Loading branch information
webmiraclepro committed Dec 27, 2015
1 parent 7ac7952 commit 976e3ed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/players/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export default class Base extends Component {
shouldComponentUpdate (nextProps) {
return this.props.url !== nextProps.url
}
isReady = false
onReady = () => {
this.setVolume(this.props.volume)
if (this.props.playing || this.preloading) {
this.preloading = false
this.isReady = true
this.play()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export default class FilePlayer extends Base {
this.player.volume = fraction
}
getFractionPlayed () {
if (this.player.readyState === 0) return 0
if (!this.isReady) return 0
return this.player.currentTime / this.player.duration
}
getFractionLoaded () {
if (this.player.readyState === 0) return 0
if (!this.isReady) return 0
return this.player.buffered.end(0) / this.player.duration
}
render () {
Expand Down
14 changes: 7 additions & 7 deletions src/players/SoundCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,31 @@ export default class SoundCloud extends Base {
ondataerror: this.props.onError
}
play () {
if (!this.player) return
if (!this.isReady) return
this.player.play()
}
pause () {
if (!this.player) return
if (!this.isReady) return
this.player.pause()
}
stop () {
if (!this.player) return
if (!this.isReady) return
this.player.stop()
}
seekTo (fraction) {
if (!this.player) return
if (!this.isReady) return
this.player.seek(this.player.getDuration() * fraction)
}
setVolume (fraction) {
if (!this.player) return
if (!this.isReady) return
this.player.setVolume(fraction)
}
getFractionPlayed () {
if (!this.player) return 0
if (!this.isReady) return 0
return this.player.getCurrentPosition() / this.player.getDuration()
}
getFractionLoaded () {
if (!this.player) return 0
if (!this.isReady) return 0
return this.player.getLoadedPosition() / this.player.getDuration()
}
render () {
Expand Down
16 changes: 8 additions & 8 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class YouTube extends Base {
}
load (url, playing) {
const id = url && url.match(MATCH_URL)[1]
if (this.player) {
if (this.isReady) {
this.stop()
if (playing) {
this.player.loadVideoById(id)
Expand Down Expand Up @@ -77,31 +77,31 @@ export default class YouTube extends Base {
if (state.data === YT.PlayerState.ENDED) this.props.onEnded()
}
play () {
if (!this.player) return
if (!this.isReady) return
this.player.playVideo()
}
pause () {
if (!this.player) return
if (!this.isReady) return
this.player.pauseVideo()
}
stop () {
if (!this.player) return
if (!this.isReady) return
this.player.stopVideo()
}
seekTo (fraction) {
if (!this.player) return
if (!this.isReady) return
this.player.seekTo(this.player.getDuration() * fraction)
}
setVolume (fraction) {
if (!this.player) return
if (!this.isReady) return
this.player.setVolume(fraction * 100)
}
getFractionPlayed () {
if (!this.player || !this.player.getCurrentTime) return 0
if (!this.isReady) return 0
return this.player.getCurrentTime() / this.player.getDuration()
}
getFractionLoaded () {
if (!this.player || !this.player.getVideoLoadedFraction) return 0
if (!this.isReady) return 0
return this.player.getVideoLoadedFraction()
}
render () {
Expand Down

0 comments on commit 976e3ed

Please sign in to comment.