| 482 | } |
| 483 | |
| 484 | initVideo(video, type) { |
| 485 | this.initMSE(video, type); |
| 486 | |
| 487 | /** |
| 488 | * video events |
| 489 | */ |
| 490 | // show video time: the metadata has loaded or changed |
| 491 | this.on('durationchange', () => { |
| 492 | // compatibility: Android browsers will output 1 or Infinity at first |
| 493 | if (video.duration !== 1 && video.duration !== Infinity) { |
| 494 | this.template.dtime.innerHTML = utils.secondToTime(video.duration); |
| 495 | } |
| 496 | }); |
| 497 | |
| 498 | // show video loaded bar: to inform interested parties of progress downloading the media |
| 499 | this.on('progress', () => { |
| 500 | const percentage = video.buffered.length ? video.buffered.end(video.buffered.length - 1) / video.duration : 0; |
| 501 | this.bar.set('loaded', percentage, 'width'); |
| 502 | }); |
| 503 | |
| 504 | // video download error: an error occurs |
| 505 | this.on('error', () => { |
| 506 | if (!this.video.error) { |
| 507 | // Not a video load error, may be poster load failed, see #307 |
| 508 | return; |
| 509 | } |
| 510 | this.tran && this.notice && this.type !== 'webtorrent' && this.notice(this.tran('video-failed')); |
| 511 | }); |
| 512 | |
| 513 | // video end |
| 514 | this.on('ended', () => { |
| 515 | this.bar.set('played', 1, 'width'); |
| 516 | if (!this.setting.loop) { |
| 517 | this.pause(); |
| 518 | } else { |
| 519 | this.seek(0); |
| 520 | this.play(); |
| 521 | } |
| 522 | if (this.danmaku) { |
| 523 | this.danmaku.danIndex = 0; |
| 524 | } |
| 525 | }); |
| 526 | |
| 527 | this.on('play', () => { |
| 528 | if (this.paused) { |
| 529 | this.play(true); |
| 530 | } |
| 531 | }); |
| 532 | |
| 533 | this.on('pause', () => { |
| 534 | if (!this.paused) { |
| 535 | this.pause(true); |
| 536 | } |
| 537 | }); |
| 538 | |
| 539 | this.on('timeupdate', () => { |
| 540 | this.bar.set('played', this.video.currentTime / this.video.duration, 'width'); |
| 541 | const currentTime = utils.secondToTime(this.video.currentTime); |