Skip to content

Instantly share code, notes, and snippets.

@astein
Last active June 20, 2016 22:55
Show Gist options
  • Save astein/e04a3882947a633721ac to your computer and use it in GitHub Desktop.
Save astein/e04a3882947a633721ac to your computer and use it in GitHub Desktop.
IMA SDK Example
package
{
import com.google.ads.ima.api.AdErrorEvent;
import com.google.ads.ima.api.AdEvent;
import com.google.ads.ima.api.AdsLoader;
import com.google.ads.ima.api.AdsManager;
import com.google.ads.ima.api.AdsManagerLoadedEvent;
import com.google.ads.ima.api.AdsRenderingSettings;
import com.google.ads.ima.api.AdsRequest;
import com.google.ads.ima.api.ViewModes;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.system.Security;
public class VASTVideoExample extends Sprite
{
private const VIDEO_WIDTH:Number = 766;
private const VIDEO_HEIGHT:Number = 431;
protected var _adTag:String;
protected var _adsLoader:AdsLoader;
protected var _adsManager:AdsManager;
public function VASTVideoExample()
{
Security.allowDomain("*");
Security.allowInsecureDomain("*");
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_adsLoader = new AdsLoader();
_adsLoader.settings.autoPlayAdBreaks = false;
_adsLoader.loadSdk();
_adsLoader.addEventListener(AdsManagerLoadedEvent.ADS_MANAGER_LOADED, adsManagerLoadedHandler);
startVideo();
}
private function adsManagerLoadedHandler(event:AdsManagerLoadedEvent):void {
var adsRenderingSettings:AdsRenderingSettings = new AdsRenderingSettings();
adsRenderingSettings.linearAdPreferred = true;
adsRenderingSettings.mimeTypes = ['application/x-shockwave-flash', 'video/x-mp4', 'video/x-flv'];
adsRenderingSettings.bitrate = 1000;
var contentPlayhead:Object = {};
contentPlayhead.time = function():Number {
return 0; // contentPlayheadTime * 1000; // in milliseconds
};
_adsManager = event.getAdsManager(contentPlayhead, adsRenderingSettings);
if (_adsManager) {
_adsManager.addEventListener(AdEvent.ALL_ADS_COMPLETED, allAdsCompletedHandler);
_adsManager.addEventListener(AdEvent.CONTENT_PAUSE_REQUESTED, contentPauseRequestedHandler);
_adsManager.addEventListener(AdEvent.CONTENT_RESUME_REQUESTED, contentResumeRequestedHandler);
_adsManager.addEventListener(AdEvent.STARTED, startedHandler);
_adsManager.addEventListener(AdErrorEvent.AD_ERROR, adsManagerPlayErrorHandler);
_adsManager.addEventListener(AdEvent.AD_BREAK_READY, adBreakReadyHandler);
_adsManager.handshakeVersion("1.0");
_adsManager.init(VIDEO_WIDTH, VIDEO_HEIGHT, ViewModes.NORMAL);
addChild(_adsManager.adsContainer);
_adsManager.start();
}
}
public function startVideo():void
{
_adTag ='https://spc--cecdifegefnefdkfkgmgcdlg.telemetryverification.net/?;referrer=;showName=;showLen=;subTagID=100;cb=' + (new Date()).getTime();
var adsRequest:AdsRequest = new AdsRequest();
adsRequest.adTagUrl = _adTag;
adsRequest.linearAdSlotWidth = VIDEO_WIDTH;
adsRequest.linearAdSlotHeight = VIDEO_HEIGHT;
adsRequest.nonLinearAdSlotWidth = VIDEO_WIDTH;
adsRequest.nonLinearAdSlotHeight = VIDEO_HEIGHT;
_adsLoader.requestAds(adsRequest);
}
public function restartVideo():void
{
//_api.track.videoReplay();
var adsRequest:AdsRequest = new AdsRequest();
adsRequest.adTagUrl = _adTag;
adsRequest.linearAdSlotWidth = VIDEO_WIDTH;
adsRequest.linearAdSlotHeight = VIDEO_HEIGHT;
adsRequest.nonLinearAdSlotWidth = VIDEO_WIDTH;
adsRequest.nonLinearAdSlotHeight = VIDEO_HEIGHT;
_adsLoader.requestAds(adsRequest);
_adsManager.start();
}
private function startedHandler(event:AdEvent):void {
// TRACK VIDEO START
// _api.track.videoStarted('vast_video');
}
private function allAdsCompletedHandler(event:AdEvent):void {
// TRACK VIDEO END
// _api.track.videoCompleted('vast_video');
// GO TO NEXT STEP OF CREATIVE
}
private function adsManagerPlayErrorHandler(event:AdEvent):void {
_adsManager.destroy();
}
private function adBreakReadyHandler(event:AdEvent):void {
// no op
}
private function contentPauseRequestedHandler(event:AdEvent):void {
// no op
}
private function contentResumeRequestedHandler(event:AdEvent):void {
// no op
}
private function adsLoadErrorHandler(event:AdEvent):void {
// no op
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment