Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen to messages before iframe append #8981

Merged
merged 14 commits into from
Apr 27, 2017
14 changes: 7 additions & 7 deletions extensions/amp-nexxtv-player/0.1/amp-nexxtv-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ class AmpNexxtvPlayer extends AMP.BaseElement {
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('allowfullscreen', 'true');
iframe.src = this.getVideoIframeSrc_();
this.element.appendChild(iframe);

this.iframe_ = iframe;

this.unlistenMessage_ = listen(this.win,'message', event => {
this.unlistenMessage_ = listen(this.win, 'message', event => {
this.handleNexxMessages_(event);
});

return this.loadPromise(this.iframe_)
.then(() => {
this.element.dispatchCustomEvent(VideoEvents.LOAD);
this.playerReadyResolver_(this.iframe_);
});
this.element.appendChild(this.iframe_);
const loadPromise = this.loadPromise(this.iframe_).then(() => {
this.element.dispatchCustomEvent(VideoEvents.LOAD);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

this.playerReadyResolver_(loadPromise);
return loadPromise;
}

pauseCallback() {
Expand Down
77 changes: 47 additions & 30 deletions extensions/amp-ooyala-player/0.1/amp-ooyala-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
installVideoManagerForDoc,
} from '../../../src/service/video-manager-impl';
import {isObject} from '../../../src/types';
import {listen} from '../../../src/event-helper';
import {VideoEvents} from '../../../src/video-interface';
import {videoManagerForDoc} from '../../../src/services';

Expand All @@ -42,6 +43,9 @@ class AmpOoyalaPlayer extends AMP.BaseElement {

/** @private {?Function} */
this.playerReadyResolver_ = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be dropped.


/** @private {?Function} */
this.unlistenMessage_ = null;
}

/**
Expand All @@ -58,16 +62,6 @@ class AmpOoyalaPlayer extends AMP.BaseElement {
this.playerReadyResolver_ = resolve;
});

const iframe = this.element.ownerDocument.createElement('iframe');
this.iframe_ = iframe;

this.forwardEvents([VideoEvents.PLAY, VideoEvents.PAUSE], iframe);
this.applyFillContent(iframe, true);
this.element.appendChild(iframe);

iframe.setAttribute('frameborder', '0');
iframe.setAttribute('allowfullscreen', 'true');

installVideoManagerForDoc(this.element);
videoManagerForDoc(this.element).register(this);
}
Expand Down Expand Up @@ -100,16 +94,25 @@ class AmpOoyalaPlayer extends AMP.BaseElement {

src += '&ec=' + encodeURIComponent(embedCode) +
'&pbid=' + encodeURIComponent(playerId);
this.iframe_.src = src;

window.addEventListener('message',
event => this.handleOoyalaMessages_(event));
const iframe = this.element.ownerDocument.createElement('iframe');
this.applyFillContent(iframe, true);
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('allowfullscreen', 'true');
iframe.src = src;

this.iframe_ = iframe;

return this.loadPromise(this.iframe_)
.then(() => {
this.element.dispatchCustomEvent(VideoEvents.LOAD);
this.playerReadyResolver_(this.iframe_);
});
this.unlistenMessage_ = listen(this.win, 'message', event => {
this.handleOoyalaMessages_(event);
});

this.element.appendChild(this.iframe_);
const loadPromise = this.loadPromise(this.iframe_).then(() => {
this.element.dispatchCustomEvent(VideoEvents.LOAD);
});
this.playerReadyResolver_(loadPromise);
return loadPromise;
}

/** @override */
Expand All @@ -118,6 +121,15 @@ class AmpOoyalaPlayer extends AMP.BaseElement {
removeElement(this.iframe_);
this.iframe_ = null;
}

if (this.unlistenMessage_) {
this.unlistenMessage_();
}

this.playerReadyPromise_ = new Promise(resolve => {
this.playerReadyResolver_ = resolve;
});

return true;
}

Expand Down Expand Up @@ -158,34 +170,39 @@ class AmpOoyalaPlayer extends AMP.BaseElement {
}
}

/**
* Sends a command to the player through postMessage.
* @param {string} command
* @private
*/
sendCommand_(command) {
this.playerReadyPromise_.then(() => {
if (this.iframe_ && this.iframe_.contentWindow) {
this.iframe_.contentWindow./*OK*/postMessage(command, '*');
}
});
}

// VideoInterface Implementation. See ../src/video-interface.VideoInterface

/** @override */
play(unusedIsAutoplay) {
this.playerReadyPromise_.then(() => {
this.iframe_.contentWindow./*OK*/postMessage('play', '*');
});
this.sendCommand_('play');
}

/** @override */
pause() {
this.playerReadyPromise_.then(() => {
this.iframe_.contentWindow./*OK*/postMessage('pause', '*');
});
this.sendCommand_('pause');
}

/** @override */
mute() {
this.playerReadyPromise_.then(() => {
this.iframe_.contentWindow./*OK*/postMessage('mute', '*');
});
this.sendCommand_('mute');
}

/** @override */
unmute() {
this.playerReadyPromise_.then(() => {
this.iframe_.contentWindow./*OK*/postMessage('unmute', '*');
});
this.sendCommand_('unmute');
}

/** @override */
Expand Down
13 changes: 5 additions & 8 deletions extensions/amp-youtube/0.1/amp-youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ class AmpYoutube extends AMP.BaseElement {
iframe.setAttribute('allowfullscreen', 'true');
iframe.src = src;
this.applyFillContent(iframe);
this.element.appendChild(iframe);

this.iframe_ = iframe;

Expand All @@ -198,16 +197,14 @@ class AmpYoutube extends AMP.BaseElement {
this.handleYoutubeMessages_.bind(this)
);

this.win.addEventListener(
'message', event => this.handleYoutubeMessages_(event)
);

return this.loadPromise(iframe)
this.element.appendChild(this.iframe_);
const loadPromise = this.loadPromise(this.iframe_)
.then(() => this.listenToFrame_())
.then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we join this with the previous block?

this.element.dispatchCustomEvent(VideoEvents.LOAD);
this.playerReadyResolver_(this.iframe_);
});
this.playerReadyResolver_(loadPromise);
return loadPromise;
}

/** @override */
Expand Down Expand Up @@ -264,7 +261,7 @@ class AmpYoutube extends AMP.BaseElement {
* @param {string} command
* @param {Array=} opt_args
* @private
* */
*/
sendCommand_(command, opt_args) {
this.playerReadyPromise_.then(() => {
if (this.iframe_ && this.iframe_.contentWindow) {
Expand Down