Skip to content

Commit

Permalink
handle play promise
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Jan 17, 2018
1 parent 6524114 commit cda9098
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 38 deletions.
40 changes: 20 additions & 20 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion dist/DPlayer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/DPlayer.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 0 additions & 5 deletions src/js/options.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
22 changes: 12 additions & 10 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cda9098

Please sign in to comment.