Skip to content

Commit

Permalink
Ensures video has sources before playing to avoid early error. (#24439)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmajoulet authored Sep 11, 2019
1 parent 4f33ab9 commit 4c0c76c
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions extensions/amp-story/1.0/amp-story-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,17 +856,28 @@ export class AmpStoryPage extends AMP.BaseElement {
*/
preloadAllMedia_() {
return this.whenAllMediaElements_((mediaPool, mediaEl) => {
if (this.isBotUserAgent_) {
// No-op.
return Promise.resolve();
} else {
return mediaPool.preload(
/** @type {!./media-pool.DomElementDef} */ (mediaEl)
);
}
this.preloadMedia_(mediaPool, mediaEl);
});
}

/**
* Preloads the given media.
* @param {!./media-pool.MediaPool} mediaPool
* @param {!Element} mediaEl
* @return {!Promise<!Element>} Promise that resolves with the preloading element.
* @private
*/
preloadMedia_(mediaPool, mediaEl) {
if (this.isBotUserAgent_) {
// No-op.
return Promise.resolve();
} else {
return mediaPool.preload(
/** @type {!./media-pool.DomElementDef} */ (mediaEl)
);
}
}

/**
* Mutes all media on this page.
* @return {!Promise} Promise that resolves after the callbacks are called.
Expand Down Expand Up @@ -1372,12 +1383,14 @@ export class AmpStoryPage extends AMP.BaseElement {

this.mediaPoolPromise_.then(mediaPool => {
if (visible) {
this.registerMedia_(mediaPool, videoEl).then(() => {
this.playMedia_(mediaPool, videoEl);
if (!this.storeService_.get(StateProperty.MUTED_STATE)) {
this.unmuteAllMedia();
}
});
this.registerMedia_(mediaPool, videoEl)
.then(() => this.preloadMedia_(mediaPool, videoEl))
.then(poolVideoEl => {
this.playMedia_(mediaPool, poolVideoEl);
if (!this.storeService_.get(StateProperty.MUTED_STATE)) {
this.unmuteAllMedia();
}
});
} else {
this.pauseMedia_(mediaPool, videoEl, true /** rewindToBeginning */);
this.muteMedia_(mediaPool, videoEl);
Expand Down

0 comments on commit 4c0c76c

Please sign in to comment.