Skip to content

Commit

Permalink
[feature] Change PlaybackRate
Browse files Browse the repository at this point in the history
  • Loading branch information
nyunyu95 authored and cookpete committed Dec 6, 2016
1 parent 397fc3b commit d28c309
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class ReactPlayer extends Component {
this.props.url !== nextProps.url ||
this.props.playing !== nextProps.playing ||
this.props.volume !== nextProps.volume ||
this.props.playbackRate !== nextProps.playbackRate ||
this.props.height !== nextProps.height ||
this.props.width !== nextProps.width ||
this.props.hidden !== nextProps.hidden
Expand Down
12 changes: 11 additions & 1 deletion src/demo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default class App extends Component {
volume: 0.8,
played: 0,
loaded: 0,
duration: 0
duration: 0,
playbackRate: 1.0
}
load = url => {
this.setState({
Expand All @@ -36,6 +37,10 @@ export default class App extends Component {
setVolume = e => {
this.setState({ volume: parseFloat(e.target.value) })
}
setPlaybackRate = e => {
console.log(parseFloat(e.target.value))
this.setState({ playbackRate: parseFloat(e.target.value) })
}
onSeekMouseDown = e => {
this.setState({ seeking: true })
}
Expand Down Expand Up @@ -76,6 +81,7 @@ export default class App extends Component {
const {
url, playing, volume,
played, loaded, duration,
playbackRate,
soundcloudConfig,
vimeoConfig,
youtubeConfig,
Expand All @@ -94,6 +100,7 @@ export default class App extends Component {
height={270}
url={url}
playing={playing}
playbackRate={playbackRate}
volume={volume}
soundcloudConfig={soundcloudConfig}
vimeoConfig={vimeoConfig}
Expand All @@ -117,6 +124,9 @@ export default class App extends Component {
<button onClick={this.stop}>Stop</button>
<button onClick={this.playPause}>{playing ? 'Pause' : 'Play'}</button>
<button onClick={this.onClickFullscreen}>Fullscreen</button>
<button onClick={this.setPlaybackRate} value={1}>1</button>
<button onClick={this.setPlaybackRate} value={1.5}>1.5</button>
<button onClick={this.setPlaybackRate} value={2}>2</button>
</td>
</tr>
<tr>
Expand Down
7 changes: 5 additions & 2 deletions src/players/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Base extends Component {
this.mounted = false
}
componentWillReceiveProps (nextProps) {
const { url, playing, volume } = this.props
const { url, playing, volume, playbackRate } = this.props
// Invoke player methods based on incoming props
if (url !== nextProps.url && nextProps.url) {
this.seekOnPlay = null
Expand All @@ -38,6 +38,8 @@ export default class Base extends Component {
this.pause()
} else if (volume !== nextProps.volume) {
this.setVolume(nextProps.volume)
} else if (playbackRate !== nextProps.playbackRate) {
this.setPlaybackRate(nextProps.playbackRate)
}
}
shouldComponentUpdate (nextProps) {
Expand All @@ -53,8 +55,9 @@ export default class Base extends Component {
}
}
onPlay = () => {
const { volume, onStart, onPlay, onDuration } = this.props
const { volume, onStart, onPlay, onDuration, playbackRate } = this.props
if (this.startOnPlay) {
this.setPlaybackRate(playbackRate)
this.setVolume(volume)
onStart()
this.startOnPlay = false
Expand Down
3 changes: 3 additions & 0 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export default class FilePlayer extends Base {
setVolume (fraction) {
this.player.volume = fraction
}
setPlaybackRate (rate) {
this.player.playbackRate = rate
}
getDuration () {
if (!this.isReady) return null
return this.player.duration
Expand Down
3 changes: 3 additions & 0 deletions src/players/Vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export default class Vimeo extends Base {
setVolume (fraction) {
this.postMessage('setVolume', fraction)
}
setPlaybackRate (rate) {
this.postMessage('setPlaybackRate', rate)
}
getDuration () {
return this.duration
}
Expand Down
4 changes: 4 additions & 0 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export default class YouTube extends Base {
if (!this.isReady || !this.player.setVolume) return
this.player.setVolume(fraction * 100)
}
setPlaybackRate (rate) {
if (!this.isReady || !this.player.setPlaybackRate) return
this.player.setPlaybackRate(rate)
}
getDuration () {
if (!this.isReady || !this.player.getDuration) return null
return this.player.getDuration()
Expand Down
2 changes: 2 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const propTypes = {
loop: bool,
controls: bool,
volume: number,
playbackRate: number,
width: oneOfType([ string, number ]),
height: oneOfType([ string, number ]),
hidden: bool,
Expand Down Expand Up @@ -45,6 +46,7 @@ export const defaultProps = {
loop: false,
controls: false,
volume: 0.8,
playbackRate: 1.0,
width: 640,
height: 360,
hidden: false,
Expand Down

0 comments on commit d28c309

Please sign in to comment.