Skip to content

Commit

Permalink
Add loop prop
Browse files Browse the repository at this point in the history
  • Loading branch information
philip-luther committed Nov 22, 2024
1 parent 774cc8d commit afe1a0b
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Prop | Description | Default
---- | ----------- | -------
`url` | The url of a video or song to play
`playing` | Set to `true` or `false` to pause or play the media | `false`
`loop` | Set to `true` or `false` to loop the media | `false`
`volume` | Sets the volume of the appropriate player | `0.8`
`width` | Sets the width of the player | `640`
`height` | Sets the height of the player | `360`
Expand Down
6 changes: 6 additions & 0 deletions src/players/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,10 @@ export default class Base extends Component {
}
}
}
onEnded = () => {
if (this.props.loop) {
this.seekTo(0)
}
this.props.onEnded()
}
}
2 changes: 1 addition & 1 deletion src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class FilePlayer extends Base {
this.player.oncanplay = this.onReady
this.player.onplay = this.onPlay
this.player.onpause = () => this.props.onPause()
this.player.onended = () => this.props.onEnded()
this.player.onended = () => this.onEnded()
this.player.onerror = e => this.props.onError(e)
this.player.setAttribute('webkit-playsinline', '')
super.componentDidMount()
Expand Down
2 changes: 1 addition & 1 deletion src/players/SoundCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class SoundCloud extends Base {
if (state === 'playing') this.onPlay()
if (state === 'paused') this.props.onPause()
if (state === 'loading') this.props.onBuffer()
if (state === 'ended') this.props.onEnded()
if (state === 'ended') this.onEnded()
}
play () {
if (!this.isReady) return
Expand Down
2 changes: 1 addition & 1 deletion src/players/Vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class Vimeo extends Base {
if (data.event === 'loadProgress') this.fractionLoaded = data.data.percent
if (data.event === 'play') this.onPlay()
if (data.event === 'pause') this.props.onPause()
if (data.event === 'finish') this.props.onEnded()
if (data.event === 'finish') this.onEnded()
if (data.method === 'getDuration') {
this.duration = data.value // Store for use later
this.onReady()
Expand Down
2 changes: 1 addition & 1 deletion src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class YouTube extends Base {
if (data === PLAYING) this.onPlay()
if (data === PAUSED) this.props.onPause()
if (data === BUFFERING) this.props.onBuffer()
if (data === ENDED) this.props.onEnded()
if (data === ENDED) this.onEnded()
if (data === CUED) this.onReady()
}
play () {
Expand Down
2 changes: 2 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PropTypes } from 'react'
export const propTypes = {
url: PropTypes.string,
playing: PropTypes.bool,
loop: PropTypes.bool,
volume: PropTypes.number,
width: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
height: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
Expand Down Expand Up @@ -31,6 +32,7 @@ export const propTypes = {

export const defaultProps = {
playing: false,
loop: false,
volume: 0.8,
width: 640,
height: 360,
Expand Down

0 comments on commit afe1a0b

Please sign in to comment.