Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented onBufferEnd cb prop for FilePlayer, YouTube and Facebook #615

Merged
merged 9 commits into from
Apr 10, 2019
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Prop | Description
`onDuration` | Callback containing duration of the media, in seconds
`onPause` | Called when media is paused
`onBuffer` | Called when media starts buffering
`onBufferEnd` | Called when media has finished buffering (works for file paths, YouTube and Facebook players)
me-andre marked this conversation as resolved.
Show resolved Hide resolved
`onSeek` | Called when media seeks with `seconds` parameter
`onEnded` | Called when media finishes playing<br />&nbsp; ◦ &nbsp;Does not fire when `loop` is set to `true`
`onError` | Called when an error occurs whilst attempting to play media
Expand Down
1 change: 1 addition & 0 deletions src/players/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Facebook extends Component {
this.player.subscribe('paused', this.props.onPause)
this.player.subscribe('finishedPlaying', this.props.onEnded)
this.player.subscribe('startedBuffering', this.props.onBuffer)
this.player.subscribe('finishedBuffering', this.props.onBufferEnd)
this.player.subscribe('error', this.props.onError)
if (!this.props.muted) {
// Player is muted by default
Expand Down
8 changes: 6 additions & 2 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ export class FilePlayer extends Component {
this.removeListeners()
}
addListeners () {
const { onReady, onPlay, onPause, onEnded, onError, playsinline, onEnablePIP } = this.props
const { onReady, onPlay, onBuffer, onBufferEnd, onPause, onEnded, onError, playsinline, onEnablePIP } = this.props
this.player.addEventListener('canplay', onReady)
this.player.addEventListener('play', onPlay)
this.player.addEventListener('waiting', onBuffer)
this.player.addEventListener('playing', onBufferEnd)
this.player.addEventListener('pause', onPause)
this.player.addEventListener('seeked', this.onSeek)
this.player.addEventListener('ended', onEnded)
Expand All @@ -82,9 +84,11 @@ export class FilePlayer extends Component {
}
}
removeListeners () {
const { onReady, onPlay, onPause, onEnded, onError, onEnablePIP } = this.props
const { onReady, onPlay, onBuffer, onBufferEnd, onPause, onEnded, onError, onEnablePIP } = this.props
this.player.removeEventListener('canplay', onReady)
this.player.removeEventListener('play', onPlay)
this.player.removeEventListener('waiting', onBuffer)
this.player.removeEventListener('playing', onBufferEnd)
this.player.removeEventListener('pause', onPause)
this.player.removeEventListener('seeked', this.onSeek)
this.player.removeEventListener('ended', onEnded)
Expand Down
7 changes: 5 additions & 2 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ export class YouTube extends Component {
}, onError)
}
onStateChange = ({ data }) => {
const { onPlay, onPause, onBuffer, onEnded, onReady, loop } = this.props
const { onPlay, onPause, onBuffer, onBufferEnd, onEnded, onReady, loop } = this.props
const { PLAYING, PAUSED, BUFFERING, ENDED, CUED } = window[SDK_GLOBAL].PlayerState
if (data === PLAYING) onPlay()
if (data === PLAYING) {
onPlay()
onBufferEnd()
}
if (data === PAUSED) onPause()
if (data === BUFFERING) onBuffer()
if (data === ENDED) {
Expand Down
2 changes: 2 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const propTypes = {
onPlay: func,
onPause: func,
onBuffer: func,
onBufferEnd: func,
onEnded: func,
onError: func,
onDuration: func,
Expand Down Expand Up @@ -163,6 +164,7 @@ export const defaultProps = {
onPlay: function () {},
onPause: function () {},
onBuffer: function () {},
onBufferEnd: function () {},
onEnded: function () {},
onError: function () {},
onDuration: function () {},
Expand Down
9 changes: 6 additions & 3 deletions test/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ test('load() when ready', t => {
getSDK.restore()
})

test('onStateChange() - play', async t => {
const onPlay = () => t.pass()
const instance = shallow(<YouTube url={TEST_URL} onPlay={onPlay} />).instance()
test('onStateChange() - play', t => {
const called = {}
const onPlay = () => { called.onPlay = true }
const onBufferEnd = () => { called.onBufferEnd = true }
const instance = shallow(<YouTube url={TEST_URL} onPlay={onPlay} onBufferEnd={onBufferEnd} />).instance()
instance.onStateChange({ data: 'PLAYING' })
t.true(called.onPlay && called.onBufferEnd)
})

test('onStateChange() - pause', async t => {
Expand Down