From ef17dcc732d282f027aa3bddf8f91cbb05998cc8 Mon Sep 17 00:00:00 2001 From: Pete Cook Date: Mon, 28 Dec 2015 21:17:17 +0000 Subject: [PATCH] Use explicit isReady property to guard player methods Fixes https://github.com/CookPete/react-player/issues/18 --- src/players/Base.js | 2 ++ src/players/FilePlayer.js | 4 ++-- src/players/SoundCloud.js | 14 +++++++------- src/players/YouTube.js | 16 ++++++++-------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/players/Base.js b/src/players/Base.js index ec99f79e..694af1ed 100644 --- a/src/players/Base.js +++ b/src/players/Base.js @@ -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() } } diff --git a/src/players/FilePlayer.js b/src/players/FilePlayer.js index 61057038..61fcdaff 100644 --- a/src/players/FilePlayer.js +++ b/src/players/FilePlayer.js @@ -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 () { diff --git a/src/players/SoundCloud.js b/src/players/SoundCloud.js index 9ab7c896..8aea4da2 100644 --- a/src/players/SoundCloud.js +++ b/src/players/SoundCloud.js @@ -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 () { diff --git a/src/players/YouTube.js b/src/players/YouTube.js index b9b0e29e..52d0890c 100644 --- a/src/players/YouTube.js +++ b/src/players/YouTube.js @@ -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) @@ -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 () {