Browse Source

handle play promise

DIYgod 7 years ago
parent
commit
cda90989b2
6 changed files with 33 additions and 36 deletions
  1. 20 20
      demo/demo.js
  2. 0 0
      dist/DPlayer.min.js
  3. 0 0
      dist/DPlayer.min.js.map
  4. 1 1
      package.json
  5. 0 5
      src/js/options.js
  6. 12 10
      src/js/player.js

+ 20 - 20
demo/demo.js

@@ -149,26 +149,26 @@ function initPlayers () {
     //     }
     // });
 
-    window.dp6 = new DPlayer({
-        container: document.getElementById('dplayer6'),
-        preload: 'none',
-        live: true,
-        danmaku: true,
-        apiBackend: {
-            read: function (endpoint, callback) {
-                console.log('假装 WebSocket 连接成功');
-                callback();
-            },
-            send: function (endpoint, danmakuData, callback) {
-                console.log('假装通过 WebSocket 发送数据', danmakuData);
-                callback();
-            }
-        },
-        video: {
-            url: 'https://moeplayer.b0.upaiyun.com/dplayer/hls/hikarunara.m3u8',
-            type: 'hls'
-        }
-    });
+    // window.dp6 = new DPlayer({
+    //     container: document.getElementById('dplayer6'),
+    //     preload: 'none',
+    //     live: true,
+    //     danmaku: true,
+    //     apiBackend: {
+    //         read: function (endpoint, callback) {
+    //             console.log('假装 WebSocket 连接成功');
+    //             callback();
+    //         },
+    //         send: function (endpoint, danmakuData, callback) {
+    //             console.log('假装通过 WebSocket 发送数据', danmakuData);
+    //             callback();
+    //         }
+    //     },
+    //     video: {
+    //         url: 'https://moeplayer.b0.upaiyun.com/dplayer/hls/hikarunara.m3u8',
+    //         type: 'hls'
+    //     }
+    // });
 }
 
 function clearPlayers () {

File diff suppressed because it is too large
+ 0 - 0
dist/DPlayer.min.js


File diff suppressed because it is too large
+ 0 - 0
dist/DPlayer.min.js.map


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "dplayer",
-  "version": "1.19.1",
+  "version": "1.20.0",
   "description": "Wow, such a lovely HTML5 danmaku video player",
   "main": "dist/DPlayer.min.js",
   "style": "dist/DPlayer.min.css",

+ 0 - 5
src/js/options.js

@@ -1,12 +1,7 @@
 /* global DPLAYER_VERSION GIT_HASH */
 import defaultApiBackend from './api.js';
-import utils from './utils';
 
 export default (options) => {
-    // compatibility: some mobile browsers don't suppose autoplay
-    if (utils.isMobile) {
-        options.autoplay = false;
-    }
 
     // default options
     const defaultOption = {

+ 12 - 10
src/js/player.js

@@ -83,7 +83,7 @@ class DPlayer {
                         this.template.danmakuLoading.style.display = 'none';
 
                         // autoplay
-                        if (this.options.autoplay && !utils.isMobile) {
+                        if (this.options.autoplay) {
                             this.play();
                         }
                         else if (utils.isMobile) {
@@ -132,14 +132,8 @@ class DPlayer {
 
         this.initVideo(this.video, this.quality && this.quality.type || this.options.video.type);
 
-        if (!this.danmaku) {
-            // autoplay
-            if (this.options.autoplay && !utils.isMobile) {
-                this.play();
-            }
-            else if (utils.isMobile) {
-                this.pause();
-            }
+        if (!this.danmaku && this.options.autoplay) {
+            this.play();
         }
 
         index++;
@@ -181,7 +175,15 @@ class DPlayer {
 
         this.template.playButton.innerHTML = this.icons.get('pause');
 
-        this.video.play();
+        const playedPromise = this.video.play();
+        if (playedPromise) {
+            playedPromise.
+                catch(() => {
+                    this.pause();
+                }).
+                then(() => {
+                });
+        }
         this.time.enable();
         this.container.classList.add('dplayer-playing');
         if (this.danmaku) {

Some files were not shown because too many files changed in this diff