Skip to content

Commit

Permalink
Use hls.js manifest parsed event for onReady
Browse files Browse the repository at this point in the history
Seems like there was some weirdness with certain HLS streams not triggering the `canplay` event, which was required for calling onReady and enabling autoplay
Fixes #1066
  • Loading branch information
cookpete committed Nov 16, 2020
1 parent 0c1bfd1 commit 5d6896b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class FilePlayer extends Component {

componentDidUpdate (prevProps) {
if (this.shouldUseAudio(this.props) !== this.shouldUseAudio(prevProps)) {
this.removeListeners(this.prevPlayer)
this.removeListeners(this.prevPlayer, prevProps.url)
this.addListeners(this.player)
}
}
Expand All @@ -43,8 +43,7 @@ export default class FilePlayer extends Component {
}

addListeners (player) {
const { playsinline } = this.props
player.addEventListener('canplay', this.onReady)
const { url, playsinline } = this.props
player.addEventListener('play', this.onPlay)
player.addEventListener('waiting', this.onBuffer)
player.addEventListener('playing', this.onBufferEnd)
Expand All @@ -55,14 +54,17 @@ export default class FilePlayer extends Component {
player.addEventListener('enterpictureinpicture', this.onEnablePIP)
player.addEventListener('leavepictureinpicture', this.onDisablePIP)
player.addEventListener('webkitpresentationmodechanged', this.onPresentationModeChange)
if (!this.shouldUseHLS(url)) { // onReady is handled by hls.js
player.addEventListener('canplay', this.onReady)
}
if (playsinline) {
player.setAttribute('playsinline', '')
player.setAttribute('webkit-playsinline', '')
player.setAttribute('x5-playsinline', '')
}
}

removeListeners (player) {
removeListeners (player, url) {
player.removeEventListener('canplay', this.onReady)
player.removeEventListener('play', this.onPlay)
player.removeEventListener('waiting', this.onBuffer)
Expand All @@ -74,6 +76,9 @@ export default class FilePlayer extends Component {
player.removeEventListener('enterpictureinpicture', this.onEnablePIP)
player.removeEventListener('leavepictureinpicture', this.onDisablePIP)
player.removeEventListener('webkitpresentationmodechanged', this.onPresentationModeChange)
if (!this.shouldUseHLS(url)) { // onReady is handled by hls.js
player.removeEventListener('canplay', this.onReady)
}
}

// Proxy methods to prevent listener leaks
Expand Down Expand Up @@ -148,6 +153,9 @@ export default class FilePlayer extends Component {
if (this.shouldUseHLS(url)) {
getSDK(HLS_SDK_URL.replace('VERSION', hlsVersion), HLS_GLOBAL).then(Hls => {
this.hls = new Hls(hlsOptions)
this.hls.on(Hls.Events.MANIFEST_PARSED, () => {
this.props.onReady()
})
this.hls.on(Hls.Events.ERROR, (e, data) => {
this.props.onError(e, data, this.hls, Hls)
})
Expand Down
4 changes: 3 additions & 1 deletion test/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ test('onError - hls', t => {
class Hls {
static Events = { ERROR: 'ERROR' }
on = (event, cb) => {
setTimeout(cb, 100)
if (event === 'ERROR') {
setTimeout(cb, 100)
}
}

loadSource = () => null
Expand Down

0 comments on commit 5d6896b

Please sign in to comment.