Skip to content

Commit

Permalink
fix: Updated samples to work on iOS. (googleads#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnbuso authored May 16, 2018
1 parent b87735f commit a21b544
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
11 changes: 8 additions & 3 deletions examples/advanced/ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ var Ads = function() {

// Start ads when the video player is clicked, but only the first time it's
// clicked.
var startEvent = 'click';
this.startEvent = 'click';
if (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i)) {
startEvent = 'touchend';
this.startEvent = 'touchend';
}
this.player.one(startEvent, this.init.bind(this));


this.wrapperDiv = document.getElementById('content_video');
this.boundInit = this.init.bind(this);
this.wrapperDiv.addEventListener(this.startEvent, this.boundInit);

var options = {
id: 'content_video',
Expand All @@ -66,6 +70,7 @@ Ads.prototype.init = function() {
this.player.ima.initializeAdDisplayContainer();
this.player.ima.setContentWithAdTag(null, this.adTagInput.value, false);
this.player.ima.requestAds();
this.wrapperDiv.removeEventListener(this.startEvent, this.boundInit);
}
};

Expand Down
21 changes: 20 additions & 1 deletion examples/autoplay/ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

var autoplayAllowed = false;
var autoplayRequiresMute = false;
var player;
var wrapperDiv;

function checkUnmutedAutoplaySupport() {
canAutoplay
Expand Down Expand Up @@ -58,7 +60,7 @@ function initPlayer() {
muted: autoplayRequiresMute,
debug: true
}
var player = videojs('content_video', vjsOptions);
player = videojs('content_video', vjsOptions);

var imaOptions = {
id: 'content_video',
Expand All @@ -70,6 +72,23 @@ function initPlayer() {

};
player.ima(imaOptions);

if (!autoplayAllowed) {
if (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i)) {
startEvent = 'touchend';
}

wrapperDiv = document.getElementById('content_video');
wrapperDiv.addEventListener(startEvent, initAdDisplayContainer);
}
}

function initAdDisplayContainer() {
player.ima.initializeAdDisplayContainer();
wrapperDiv.removeEventListener(startEvent, initAdDisplayContainer);
}

var startEvent = 'click';
checkUnmutedAutoplaySupport();
21 changes: 14 additions & 7 deletions examples/multiple/ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function pause(id) {
var Player = function(id) {
this.id = id;
this.init = function() {
var player = videojs(this.id);
this.player = videojs(this.id);

var options = {
id: id,
Expand All @@ -33,7 +33,7 @@ var Player = function(id) {
'vid=short_onecue&correlator='
};

player.ima(options);
this.player.ima(options);

// Remove controls from the player on iPad to stop native controls from stealing
// our click
Expand All @@ -46,16 +46,23 @@ var Player = function(id) {

// Initialize the ad container when the video player is clicked, but only the
// first time it's clicked.
var startEvent = 'click';
this.startEvent = 'click';
if (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i)) {
startEvent = 'touchend';
this.startEvent = 'touchend';
}

player.one(startEvent, function() {
player.ima.initializeAdDisplayContainer();
});
this.wrapperDiv = document.getElementById(this.id);
this.boundInitAdDisplayContainer = this.initAdDisplayContainer.bind(this);
this.wrapperDiv.addEventListener(
this.startEvent, this.boundInitAdDisplayContainer);
}

this.initAdDisplayContainer = function() {
this.player.ima.initializeAdDisplayContainer();
this.wrapperDiv.removeEventListener(
this.startEvent, this.boundInitAdDisplayContainer);
}
}

Expand Down
10 changes: 7 additions & 3 deletions examples/playlist/ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ var Ads = function() {

// Start ads when the video player is clicked, but only the first time it's
// clicked.
var startEvent = 'click';
this.startEvent = 'click';
if (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i)) {
startEvent = 'touchend';
this.startEvent = 'touchend';
}
this.player.one(startEvent, this.initFromStart.bind(this));
this.wrapperDiv = document.getElementById('content_video');
this.boundInitFromStart = this.initFromStart.bind(this);
this.wrapperDiv.addEventListener(this.startEvent, this.initFromStart.bind(this));

this.options = {
id: 'content_video',
Expand Down Expand Up @@ -77,6 +79,8 @@ var Ads = function() {
Ads.prototype.initFromStart = function() {
if (!this.initialized) {
this.init();
this.wrapperDiv.removeEventListener(
this.startEvent, this.boundInitFromStart);
}
}

Expand Down
12 changes: 9 additions & 3 deletions examples/simple/ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ if ((navigator.userAgent.match(/iPad/i) ||

// Initialize the ad container when the video player is clicked, but only the
// first time it's clicked.
var initAdDisplayContainer = function() {
player.ima.initializeAdDisplayContainer();
wrapperDiv.removeEventListener(startEvent, initAdDisplayContainer);
}

var startEvent = 'click';
if (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i)) {
startEvent = 'touchend';
}
player.one(startEvent, function() {
player.ima.initializeAdDisplayContainer();
});

var wrapperDiv = document.getElementById('content_video');
wrapperDiv.addEventListener(startEvent, initAdDisplayContainer);

0 comments on commit a21b544

Please sign in to comment.