Handle when autoplay fails
authorChocobozzz <me@florianbigard.com>
Wed, 18 Apr 2018 08:20:13 +0000 (10:20 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 18 Apr 2018 08:20:13 +0000 (10:20 +0200)
client/src/app/videos/+video-watch/video-watch.component.scss
client/src/assets/player/peertube-player.ts
client/src/assets/player/peertube-videojs-plugin.ts
client/src/assets/player/peertube-videojs-typings.ts

index 3ebeccd4bcf2032036ba7ebf2b305c5c30c6455a..8bc9f8d9a816cd3e66927a0d90d4c5a20a966b02 100644 (file)
 
         .video-actions-rates {
           margin-top: 20px;
-          align-items: left;
+          align-items: start;
 
           .video-info-likes-dislikes-bar {
             margin-top: 10px;
index e8a258065e482e1ae36b52a32393ba8cc87df239..f02fe5d75574a086313b36cccf05959e7f3056d1 100644 (file)
@@ -27,11 +27,12 @@ function getVideojsOptions (options: {
   const videojsOptions = {
     controls: true,
     poster: options.poster,
-    autoplay: options.autoplay,
+    autoplay: false,
     inactivityTimeout: options.inactivityTimeout,
     playbackRates: [ 0.5, 1, 1.5, 2 ],
     plugins: {
       peertube: {
+        autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
         videoFiles: options.videoFiles,
         playerElement: options.playerElement,
         videoViewUrl: options.videoViewUrl,
index 60c291a50fc4c6f7e856c0ade89444ed708616c9..290d88724f063bbd914b783332f56f54ca5d3a69 100644 (file)
@@ -68,9 +68,7 @@ class PeerTubePlugin extends Plugin {
   constructor (player: videojs.Player, options: PeertubePluginOptions) {
     super(player, options)
 
-    // Fix canplay event on google chrome by disabling default videojs autoplay
-    this.autoplay = this.player.options_.autoplay
-    this.player.options_.autoplay = false
+    this.autoplay = options.autoplay
 
     this.startTime = options.startTime
     this.videoFiles = options.videoFiles
@@ -190,12 +188,7 @@ class PeerTubePlugin extends Plugin {
 
           if (err) return this.fallbackToHttp(done)
 
-          if (!this.player.paused()) {
-            const playPromise = this.player.play()
-            if (playPromise !== undefined) return playPromise.then(done)
-
-            return done()
-          }
+          if (!this.player.paused()) return this.tryToPlay(done)
 
           return done()
         })
@@ -264,6 +257,25 @@ class PeerTubePlugin extends Plugin {
     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) {
     this.player.currentTime(time)
     this.player.handleTechSeeked_()
@@ -317,7 +329,10 @@ class PeerTubePlugin extends Plugin {
     if (this.autoplay === true) {
       this.player.posterImage.hide()
 
-      this.updateVideoFile(undefined, 0, () => this.seek(this.startTime))
+      this.updateVideoFile(undefined, 0, () => {
+        this.seek(this.startTime)
+        this.tryToPlay()
+      })
     } else {
       // Proxy first play
       const oldPlay = this.player.play.bind(this.player)
index a66caa30beffad446d898f37f6b60b8793d86e17..abdf333e11244f4ec5d44f80dc6db4d4421a26ab 100644 (file)
@@ -22,6 +22,7 @@ type PeertubePluginOptions = {
   videoViewUrl: string
   videoDuration: number
   startTime: number
+  autoplay: boolean
 }
 
 // videojs typings don't have some method we need