Skip to content

Commit

Permalink
fix: force autoplay in Chrome (#4804)
Browse files Browse the repository at this point in the history
Chrome has started pausing autoplaying video elements when they are
moved in the DOM. Here we need to make sure that if the video started
autoplaying, after we move the element in the DOM we call play again.

Fixes #4720.
  • Loading branch information
gkatsev authored Dec 14, 2017
1 parent 409a13e commit 6fe7a9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ class Player extends Component {
this.changingSrc_ = false;
this.playWaitingForReady_ = false;
this.playOnLoadstart_ = null;

this.forceAutoplayInChrome_();
}

/**
Expand Down Expand Up @@ -2557,11 +2559,29 @@ class Player extends Component {
if (value !== undefined) {
this.techCall_('setAutoplay', value);
this.options_.autoplay = value;
this.ready(this.forceAutoplayInChrome_);
return;
}
return this.techGet_('autoplay', value);
}

/**
* chrome started pausing the video when moving in the DOM
* causing autoplay to not continue due to how Video.js functions.
* See #4720 for more info.
*
* @private
*/
forceAutoplayInChrome_() {
if (this.paused() &&
// read from the video element or options
(this.autoplay() || this.options_.autoplay) &&
// only target desktop chrome
(browser.IS_CHROME && !browser.IS_ANDROID)) {
this.play();
}
}

/**
* Set or unset the playsinline attribute.
* Playsinline tells the browser that non-fullscreen playback is preferred.
Expand Down
5 changes: 5 additions & 0 deletions test/unit/tech/tech-faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TechFaker extends Tech {

setMuted() {}

setAutoplay() {}

currentTime() {
return 0;
}
Expand All @@ -54,6 +56,9 @@ class TechFaker extends Tech {
muted() {
return false;
}
autoplay() {
return false;
}
pause() {
return false;
}
Expand Down

0 comments on commit 6fe7a9a

Please sign in to comment.