Hls.js: Add loading indicator while video is buffering

Created on 3 Feb 2019  路  2Comments  路  Source: video-dev/hls.js

Hello, I planned to switch from Shaka player to HLS.js

In Shaka, there is a buffering event I can use to show the loading spinner in my custom player.

this.player.addEventListener('buffering', (event) => {
  this.isBuffering = event.buffering
})

But in HLS.js, I cannot find similar events so far, what I tried so far is:

this.player.on(window.Hls.Events.FRAG_LOADING, () => {
  this.isBuffering = true
})
this.player.on(window.Hls.Events.BUFFER_APPENDING, () => {
  this.isBuffering = true
})
this.player.on(window.Hls.Events.FRAG_PARSING_DATA, () => {
  this.isBuffering = true
})
this.player.on(window.Hls.Events.BUFFER_APPENDED, () => {
  this.isBuffering = false
})
this.player.on(window.Hls.Events.FRAG_PARSED, () => {
  this.isBuffering = false
})

But this is buggy, the spinner will still keep even the video is playing
Thanks for any help

Most helpful comment

@itsjamie Hi, thanks for your help, I got it work now!

this.player.on(window.Hls.Events.ERROR, (event, { details }) => {
  if (details === window.Hls.ErrorDetails.BUFFER_STALLED_ERROR) {
    this.isBuffering = true
  }
})
this.player.on(window.Hls.Events.FRAG_BUFFERED, () => {
  this.isBuffering = false
})

All 2 comments

Hello, a section of the documentation that might help is: https://github.com/video-dev/hls.js/blob/master/docs/API.md#media-errors.

The BUFFER_STALLED event may be helpful to you.

@itsjamie Hi, thanks for your help, I got it work now!

this.player.on(window.Hls.Events.ERROR, (event, { details }) => {
  if (details === window.Hls.ErrorDetails.BUFFER_STALLED_ERROR) {
    this.isBuffering = true
  }
})
this.player.on(window.Hls.Events.FRAG_BUFFERED, () => {
  this.isBuffering = false
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nickcartery picture nickcartery  路  4Comments

phillydogg28 picture phillydogg28  路  4Comments

jlacivita picture jlacivita  路  3Comments

crazytoad picture crazytoad  路  3Comments

dan-ziv picture dan-ziv  路  4Comments