Skip to content

Commit

Permalink
Allow seekTo to accept number of seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Webmaster1116 committed Jul 27, 2017
1 parent 563abe6 commit e11c8e9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Use [`ref`](https://facebook.github.io/react/docs/refs-and-the-dom.html) to call

Prop | Description
---- | -----------
`seekTo(fraction)` | Seek to the specified fraction (from 0 to 1) of the currently playing media
`seekTo(amount)` | Seek to the given number of seconds, or fraction if `amount` is between `0` and `1`.
`getCurrentTime()` | Returns the number of seconds that has been played.<br >Returns `null` if duration is unavailable.
`getDuration()` | Returns the duration (in seconds) of the currently playing media.<br >Returns `null` if duration is unavailable.

Expand Down
12 changes: 9 additions & 3 deletions src/players/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ export default class Base extends Component {
shouldComponentUpdate (nextProps) {
return this.props.url !== nextProps.url
}
seekTo (fraction) {
seekTo (amount) {
// When seeking before player is ready, store value and seek later
if (!this.isReady && fraction !== 0) {
this.seekOnPlay = fraction
if (!this.isReady && amount !== 0) {
this.seekOnPlay = amount
setTimeout(() => {
this.seekOnPlay = null
}, SEEK_ON_PLAY_EXPIRY)
}
// Return the seconds to seek to
if (amount > 0 && amount < 1) {
// Convert fraction to seconds based on duration
return this.getDuration() * amount
}
return amount
}
onPlay = () => {
const { volume, onStart, onPlay, onDuration, playbackRate } = this.props
Expand Down
6 changes: 3 additions & 3 deletions src/players/DailyMotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ export default class DailyMotion extends Base {
stop () {
// Nothing to do
}
seekTo (fraction) {
super.seekTo(fraction)
seekTo (amount) {
const seconds = super.seekTo(amount)
if (!this.isReady || !this.player.seek) return
this.player.seek(this.getDuration() * fraction)
this.player.seek(seconds)
}
setVolume (fraction) {
if (!this.isReady || !this.player.setVolume) return
Expand Down
6 changes: 3 additions & 3 deletions src/players/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export default class YouTube extends Base {
stop () {
// No need to stop
}
seekTo (fraction) {
super.seekTo(fraction)
seekTo (amount) {
const seconds = super.seekTo(amount)
if (!this.isReady) return
this.player.seek(this.getDuration() * fraction)
this.player.seek(seconds)
}
setVolume (fraction) {
if (!this.isReady) return
Expand Down
9 changes: 3 additions & 6 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ export default class FilePlayer extends Base {
this.hls.detachMedia()
}
}
seekTo (fraction) {
if (fraction === 1) {
this.pause()
}
super.seekTo(fraction)
this.player.currentTime = this.getDuration() * fraction
seekTo (amount) {
const seconds = super.seekTo(amount)
this.player.currentTime = seconds
}
setVolume (fraction) {
this.player.volume = fraction
Expand Down
6 changes: 3 additions & 3 deletions src/players/Vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export default class Vimeo extends Base {
if (!this.isReady) return
this.player.unload()
}
seekTo (fraction) {
super.seekTo(fraction)
seekTo (amount) {
const seconds = super.seekTo(amount)
if (!this.isReady || !this.player.setCurrentTime) return
this.player.setCurrentTime(this.duration * fraction)
this.player.setCurrentTime(seconds)
}
setVolume (fraction) {
this.player.setVolume(fraction)
Expand Down
6 changes: 3 additions & 3 deletions src/players/Wistia.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export default class Wistia extends Base {
if (!this.isReady || !this.player) return
this.player.pause()
}
seekTo (fraction) {
super.seekTo(fraction)
seekTo (amount) {
const seconds = super.seekTo(amount)
if (!this.isReady || !this.player) return
this.player.time(this.getDuration() * fraction)
this.player.time(seconds)
}
setVolume (fraction) {
if (!this.isReady || !this.player || !this.player.volume) return
Expand Down
6 changes: 3 additions & 3 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export default class YouTube extends Base {
if (!document.body.contains(this.player.getIframe())) return
this.player.stopVideo()
}
seekTo (fraction) {
super.seekTo(fraction)
seekTo (amount) {
const seconds = super.seekTo(amount)
if (!this.isReady || !this.player.seekTo) return
this.player.seekTo(this.getDuration() * fraction)
this.player.seekTo(seconds)
}
setVolume (fraction) {
if (!this.isReady || !this.player.setVolume) return
Expand Down

0 comments on commit e11c8e9

Please sign in to comment.