Skip to content

Commit

Permalink
Merge pull request #86 from clappr/feature/fix-buffering-and-play-events
Browse files Browse the repository at this point in the history
Fix buffering, seeked and playing events
  • Loading branch information
joaopaulovieira authored Oct 13, 2020
2 parents 7dae1b5 + 97562ba commit 0c805ce
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions src/clappr-dash-shaka-playback.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class DashShakaPlayback extends HTML5Video {
}
}

static get shakaPlayer() { return shaka }

static canPlay (resource, mimeType = '') {
shaka.polyfill.installAll()
let browserSupported = shaka.Player.isBrowserSupported()
Expand Down Expand Up @@ -141,6 +143,31 @@ class DashShakaPlayback extends HTML5Video {
this._startTimeUpdateTimer()
}

_onPlaying() {
/*
The `_onPlaying` should not be called while buffering: https://github.com/google/shaka-player/issues/2230
It will be executed on bufferfull.
*/
if (this._isBuffering) return
return super._onPlaying()
}

_onSeeking() {
this._isSeeking = true
return super._onSeeking()
}

_onSeeked() {
/*
The `_onSeeked` should not be called while buffering.
It will be executed on bufferfull.
*/
if (this._isBuffering) return

this._isSeeking = false
return super._onSeeked()
}

_startTimeUpdateTimer() {
this._stopTimeUpdateTimer()
this._timeUpdateTimer = setInterval(() => {
Expand Down Expand Up @@ -350,7 +377,7 @@ class DashShakaPlayback extends HTML5Video {
let player = new shaka.Player(this.el)
player.addEventListener('error', this._onError.bind(this))
player.addEventListener('adaptation', this._onAdaptation.bind(this))
player.addEventListener('buffering', this._onBuffering.bind(this))
player.addEventListener('buffering', this._handleShakaBufferingEvents.bind(this))
return player
}

Expand All @@ -372,10 +399,24 @@ class DashShakaPlayback extends HTML5Video {
this.trigger(Events.PLAYBACK_TIMEUPDATE, update, this.name)
}

_onBuffering (e) {
// skipping HTML5 `_handleBufferingEvents` in favor of shaka buffering events
_handleBufferingEvents() {}

_handleShakaBufferingEvents(e) {
if (this._stopped) return
let event = e.buffering ? Events.PLAYBACK_BUFFERING : Events.PLAYBACK_BUFFERFULL
this.trigger(event)

this._isBuffering = e.buffering
this._isBuffering ? this._onBuffering() : this._onBufferfull()
}

_onBuffering () {
this.trigger(Events.PLAYBACK_BUFFERING)
}

_onBufferfull() {
this.trigger(Events.PLAYBACK_BUFFERFULL)
if (this._isSeeking) this._onSeeked()
if (this.isPlaying()) this._onPlaying()
}

_loaded () {
Expand Down

0 comments on commit 0c805ce

Please sign in to comment.