Skip to content

Instantly share code, notes, and snippets.

@astein
Created February 10, 2015 01:53
Show Gist options
  • Save astein/3cd5d92e14825fc6af5a to your computer and use it in GitHub Desktop.
Save astein/3cd5d92e14825fc6af5a to your computer and use it in GitHub Desktop.
package com.truex.placements.extensions
{
import com.truex.placements.vpaid.VPAIDLoader;
import flash.display.Sprite;
import flash.events.Event;
import flash.system.Security;
import tv.freewheel.ad.behavior.IConstants;
import tv.freewheel.ad.behavior.IRenderer;
import tv.freewheel.ad.behavior.IRendererContext;
import tv.freewheel.ad.behavior.IRendererController;
public class TrueXAdRenderer extends Sprite implements IRenderer
{
private var _fwController:IRendererController;
private var _fwContext:IRendererContext;
private var _fwContainer:Sprite;
private var _constants:IConstants;
private var _width:Number;
private var _height:Number;
private var _vpaid:VPAIDLoader;
private var _credited:Boolean;
public function TrueXAdRenderer()
{
Security.allowDomain('*');
}
public function getInfo():Object
{
return {moduleType: "renderer"};
}
public function getNewInstance(rendererInterfaceVersion:int):IRenderer
{
return this;
}
public function init(context:IRendererContext, controller:IRendererController):void
{
_fwContext = context;
_fwController = controller;
_fwContainer = _fwContext.getSlotBaseDisplay();
_constants = _fwContext.getConstants();
_width = _fwContext.getSlotBaseDisplaySize().width;
_height = _fwContext.getSlotBaseDisplaySize().height;
_vpaid = new VPAIDLoader();
addChild(_vpaid);
_vpaid.addEventListener('AdLoaded', onVPAIDLoaded);
_vpaid.addEventListener('AdStopped', onVPAIDStopped);
_vpaid.addEventListener('AdError', onVPAIDError);
_vpaid.addEventListener('superTagInteract', onInteract);
_vpaid.addEventListener('superTagCredit', onCredit);
_fwController.setCapability( _constants.RENDERER_CAPABILITY_VIDEOSTATUSCONTROL, true);
_fwController.handleStateTransition(_constants.RENDERER_STATE_INITIALIZE_COMPLETE);
}
public function preload():void
{
_fwController.addAllowedDomain('*.truex.com');
_fwController.addAllowedDomain('truex.com');
_vpaid.initAd(_width, _height, 'normal', 268, JSON.stringify({"user_id":"84c432e8dbc8a09f","placement_hash":"4b735a619a7935c3f94216189533a232ffb2b80c","vast_config_url":"get.truex.com/v2/simple?partner_config_hash=4b735a619a7935c3f94216189533a232ffb2b80c&dto=vast_config"}), null);
}
protected function onVPAIDLoaded(e:Event):void
{
_fwController.handleStateTransition(_constants.RENDERER_STATE_PRELOAD_COMPLETE);
}
protected function onVPAIDError(e:Event):void
{
_fwController.handleStateTransition(_constants.RENDERER_STATE_FAIL, {fallback:true});
}
protected function onVPAIDStopped(e:Event):void
{
if (_credited) {
// USER HAS COMPLETED TRUEX AD, SKIP THE REST OF THE AD BREAK
_fwContext.getSlot().stop();
_fwController.handleStateTransition(_constants.RENDERER_STATE_STOP_COMPLETE, {fallback:true});
} else {
// IF THE USER IS NO CREDITED FOR TRUEX AD, SIGNAL ERROR TO ALLOW FALLBACK AD
_fwController.handleStateTransition(_constants.RENDERER_STATE_FAIL, {fallback:true});
}
}
protected function onCredit(e:Event):void
{
_credited = true;
}
protected function onInteract(e:Event):void
{
// IF USER CHOSES TRUEX AD, SIGNAL START
_fwController.handleStateTransition(_constants.RENDERER_STATE_PLAYING);
}
public function start():void
{
_fwContainer.addChild(this);
_vpaid.startAd();
// DO NOT SIGNAL START
//_fwController.handleStateTransition(_constants.RENDERER_STATE_PLAYING);
}
public function stop(immediate:Boolean=false):void
{
_vpaid.stopAd();
}
public function resize():void
{
_width = _fwContext.getSlotBaseDisplaySize().width;
_height = _fwContext.getSlotBaseDisplaySize().height;
_vpaid.resizeAd(_width, _height, 'normal');
}
public function getPlayheadTime():Number
{
if (_vpaid.adDuration > 0 && _vpaid.adRemainingTime > 0)
return (_vpaid.adDuration - _vpaid.adRemainingTime);
return -1;
}
public function getTotalBytes():int
{
return -1;
}
public function getBytesLoaded():int
{
return -1;
}
public function getDuration():Number
{
return -1;
}
public function setAdVolume(volume:uint):void
{
_vpaid.adVolume = volume;
}
public function pause():void
{
_vpaid.pauseAd();
}
public function resume():void
{
_vpaid.resumeAd();
}
public function dispose():void
{
if (_fwContainer.contains(this))
_fwContainer.removeChild(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment