Handle when autoplay fails

This commit is contained in:
Chocobozzz 2018-04-18 10:20:13 +02:00
parent 66dc590764
commit 80109b2ddb
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 29 additions and 12 deletions

View File

@ -332,7 +332,7 @@
.video-actions-rates { .video-actions-rates {
margin-top: 20px; margin-top: 20px;
align-items: left; align-items: start;
.video-info-likes-dislikes-bar { .video-info-likes-dislikes-bar {
margin-top: 10px; margin-top: 10px;

View File

@ -27,11 +27,12 @@ function getVideojsOptions (options: {
const videojsOptions = { const videojsOptions = {
controls: true, controls: true,
poster: options.poster, poster: options.poster,
autoplay: options.autoplay, autoplay: false,
inactivityTimeout: options.inactivityTimeout, inactivityTimeout: options.inactivityTimeout,
playbackRates: [ 0.5, 1, 1.5, 2 ], playbackRates: [ 0.5, 1, 1.5, 2 ],
plugins: { plugins: {
peertube: { peertube: {
autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
videoFiles: options.videoFiles, videoFiles: options.videoFiles,
playerElement: options.playerElement, playerElement: options.playerElement,
videoViewUrl: options.videoViewUrl, videoViewUrl: options.videoViewUrl,

View File

@ -68,9 +68,7 @@ class PeerTubePlugin extends Plugin {
constructor (player: videojs.Player, options: PeertubePluginOptions) { constructor (player: videojs.Player, options: PeertubePluginOptions) {
super(player, options) super(player, options)
// Fix canplay event on google chrome by disabling default videojs autoplay this.autoplay = options.autoplay
this.autoplay = this.player.options_.autoplay
this.player.options_.autoplay = false
this.startTime = options.startTime this.startTime = options.startTime
this.videoFiles = options.videoFiles this.videoFiles = options.videoFiles
@ -190,12 +188,7 @@ class PeerTubePlugin extends Plugin {
if (err) return this.fallbackToHttp(done) if (err) return this.fallbackToHttp(done)
if (!this.player.paused()) { if (!this.player.paused()) return this.tryToPlay(done)
const playPromise = this.player.play()
if (playPromise !== undefined) return playPromise.then(done)
return done()
}
return done() return done()
}) })
@ -264,6 +257,25 @@ class PeerTubePlugin extends Plugin {
this.trigger('autoResolutionUpdate') this.trigger('autoResolutionUpdate')
} }
private tryToPlay (done?: Function) {
if (!done) done = function () { /* empty */ }
const playPromise = this.player.play()
if (playPromise !== undefined) {
return playPromise.then(done)
.catch(err => {
console.error(err)
this.player.pause()
this.player.posterImage.show()
this.player.removeClass('vjs-has-autoplay')
return done()
})
}
return done()
}
private seek (time: number) { private seek (time: number) {
this.player.currentTime(time) this.player.currentTime(time)
this.player.handleTechSeeked_() this.player.handleTechSeeked_()
@ -317,7 +329,10 @@ class PeerTubePlugin extends Plugin {
if (this.autoplay === true) { if (this.autoplay === true) {
this.player.posterImage.hide() this.player.posterImage.hide()
this.updateVideoFile(undefined, 0, () => this.seek(this.startTime)) this.updateVideoFile(undefined, 0, () => {
this.seek(this.startTime)
this.tryToPlay()
})
} else { } else {
// Proxy first play // Proxy first play
const oldPlay = this.player.play.bind(this.player) const oldPlay = this.player.play.bind(this.player)

View File

@ -22,6 +22,7 @@ type PeertubePluginOptions = {
videoViewUrl: string videoViewUrl: string
videoDuration: number videoDuration: number
startTime: number startTime: number
autoplay: boolean
} }
// videojs typings don't have some method we need // videojs typings don't have some method we need