Creating an Ad Rules SWF requires advanced development skills. This feature is intended for use by ad network and ad serving partners with complex ad delivery requirements. If you are a Brightcove customer and think your project or product calls for use of the Brightcove Ad Rules SWF, please first contact your Account Manager to see if one of Brightcove's ad partners would work better for you.
Do you need to have more control over your ad delivery, even more control than you can get using the Brightcove Advertising module? If yes, then read on! This document introduces the Brightcove Ad Rules SWF feature, which you can use to customize the delivery time and the rendering of ads in players. The first half of this article describes your ad delivery options and the advantageous features of the Ad Rules SWF. The second half offers technical instructions on how to create and use your Ad Rules SWF.
There are several ways to set ad delivery timing for a Brightcove player. Let's review a few of the options available to Brightcove customers before we dive into the Ad Rules feature:
All three of the solutions described above use the built-in Brightcove ad request and ad rendering capabilities. The Ad Rules SWF is an alternative solution that makes it much easier to integrate with third party ad libraries that control all aspects of advertising in Brightcove video players. The Brightcove Ad Rules feature provides a solution for the most complicated ad integrations by giving developers significant flexibility, while still allowing use of other key Brightcove features, like Ad Translators and Ad SWFs. With the Ad Rules SWF, you can create a complete custom ad integration and apply it to as many Brightcove players as you would like. Here's the basics of how the Ad Rules SWF works:

Several partners have already taken advantage of the Ad Rules SWF in order to integrate with Brightcove players. The Tremor Media video monetization platform, Acudeo, has been integrated with Brightcove players using the Ad Rules SWF feature. Brightcove also integrated Yahoo's newest digital advertising platform, APT, using the Ad Rules SWF. If you intend to use the Ad Rules SWF to integrate another third party ad library and delivery system, be sure to contact your Account Manager or Customer Support first to see if Brightcove already has plans to partner with that particular third party.
Now, let's walk through an example Ad Rules application. Before you begin, be sure to read over Developing Ad Translators and Developing Ad SWFs. It is helpful to thoroughly understand both Ad Translators and Ad SWFs (and what they are used for) before beginning work on an Ad Rules SWF. You'll want to carefully analyze how all the Brightcove features work together in Ad Rules SWFs since, for some integrations, one of the simpler Brightcove ad policy solutions listed above may be sufficient. For the more complex integration, however, using a flexible Ad Rules SWF is definitely the way to go.
To help you learn how to write an Ad Rules SWF, you can download a zip file, AdRulesDevKit.zip, that includes the following resources:
To learn about creating and using Ad Rules SWFs, set up the example and see how it works. Here are the main steps:
To try out the example Ad Rules SWF, you can either compile ExampleAdRules.as or just use the already-compiled Example.swf. To start with ExampleAdRules.as:
To use the already-compiled Example.swf:
Now that we have compiled and hosted the Ad Rules SWF, we can use it to override an ad policy in a player.
Flash security features constrain a Flash player's ability to interact with files on other domains. You need to modify your site's crossdomain.xml file to permit your players to access the domain that hosts your Ad Rules SWF. If your Ad Rules SWF is hosted on http://example.com, include this line in your crossdomain.xml file:
<allow-access-from domain="example.com" />
You could also choose to add Security.allowDomain("admin.
Let's use ExampleAdRules.as as a starting point for understanding how the Ad Rules SWF works. The example SWF uses an ad translator to load an ad SWF which renders a pre-roll ad every time a video is played. Very little code is necessary for both ExampleAdRules.as and ExampleTranslatorWithAdSwf.as — most of the ad serving logic is in ExampleAdSwfForAdRules.as. You could choose to bypass the ad translator route and put all ad serving logic in the main Ad Rules class itself, but the Ad SWF framework allows you to integrate directly with Brightcove players, which makes development much easier. For example, you can set configuration properties in ad SWFs that will allow the player to re-size the ad automatically for you in full screen or pause the video player when the ad renders. The comments in all three classes should help you understand what is going on.
Let's walk through several of the key components of the example Ad Rules SWF:
override public function setAdRulesContext(adContext:AdRulesContext):void { this.adContext = adContext; // before we do anything else, we load the API modules for potential use
adContext.moduleLoader.addEventListener(Event.COMPLETE, modulesLoaded); adContext.moduleLoader.loadModules(); }
In the example, we wait to load the player API until after our ad translator is ready. So, we use two more functions in AdRules - setAdTranslator() and adTranslatorLoaded(). The ad translator is compiled in in setAdRulesContext() so that the translator is immediately initialized. You could also load the translator if it is hosted somewhere else by using loadAdTranslator(). If you choose to set or load an ad translator you must override the adTranslatorLoaded() function as this function is triggered once the translator is finished loading and is ready for interaction.
We also initialize our ad SWF in setAdRulesContext() and pass that instance to our ad translator. This allows us to have access to the Ad SWF instance in the Ad Rules SWF but still handle all the ad display logic in the Ad SWF. Once the Player API has been loaded and is ready to be used (see 'modulesLoaded' below), then we pass the Ad API reference to the Ad SWF so that the API doesn't have to be loaded or accessed again. This just cuts down on code duplication between the different ad tools. In this example, after you have access to the player API, your Ad Rules SWF is ready to be used by the player. At this point, you must call adRulesReady(), as shown below, to tell the player that the Ad Rules SWF is ready for interaction. If you forget, or just fail to call, this function, your player will hang, since it hasn't been told by the Ad Rules SWF to move on.
/** * Set the ad rules context object. */ override public function setAdRulesContext(pAdRulesContext:AdRulesContext):void { context = pAdRulesContext; // Set the ExampleTranslatorWithAdSwf translator as current translator by compiling it in.
// Pass the ad SWF reference to the ad translator for display. exampleAdSwf = new ExampleAdSwfForAdRules();
setAdTranslator(new ExampleTranslatorWithAdSwf(exampleAdSwf)); // Instead of setting the translator directly, you could also call
// loadAdTranslator() here to load the translator from a URL.
// adTranslatorLoaded() will still be fired once the translator is
// loaded and ready for interaction.
// loadAdTranslator("http://example.com/adtranslator.swf");
} /** * Override the handler in AdRules that indicates whether or * not the translator has finished loading. Load the player API * modules now. */ override public function adTranslatorLoaded():void { adContext.moduleLoader.addEventListener(Event.COMPLETE, modulesLoaded); adContext.moduleLoader.loadModules(); } /** * Called when the API modules have been loaded. */ private function modulesLoaded(pEvent:Event):void { context.moduleLoader.removeEventListener(Event.COMPLETE, modulesLoaded); // now that the APIs are loaded in we can get the modules
adAPI = context.moduleLoader.getModule("advertising"); // pass the advertising API module to the ad SWF so the API doesn't have to
// be loaded every time an ad call is made.
exampleAdSwf.adAPI = adAPI; // adRulesReady() MUST be called at some point in your ad SWF // or the player will hang. Calling this function tells the player // that the ad rules SWF is ready. In this simple case, we wanted to make sure // all our API modules were loaded and ready to be used before we tell the // player that our SWF is ready. adRulesReady(); }
You can also set up ad insertions in the ad rules SWF. In this example, we always want to show a pre-roll ad before the video content starts playing back as well as a post-roll ad once the video content is done playing. We make use of the ad insertion API which is documented in AdRules.
All we need to do is override both the prerollCheck() and postrollCheck() methods in AdRules. Then, we call callAdTranslator() to trigger the ad request and we call prerollCheckComplete() to let the player know that we are done checking for that particular insertion point. Using these APIs allows you to get and display an ad exactly when you want so any timing issues can be avoided. Other APIs like midrollCheck(), midrollCheckComplete(), onLoadCheck(), and onLoadCheckComplete() are also available by extending AdRules. See ExampleAdRules.as to view an example of how to use the mid-roll insertion API and DFPExample.as to view an example of using the onload insertion API.
/** * Override the AdRules method. This is called whenever a pre-roll * ad can be played. For this example, we want to play both pre-rolls and * post-rolls on every video so execute 'callAdTranslator' here to play an * ad at the pre-roll insertion, right before the content starts to play * back. */ override public function prerollCheck():void { // callAdTranslator triggers "fetchAd" in ExampleTranslatorWithAdSwf.as
callAdTranslator(); // Let the player know that we are done checking for a pre-roll ad.
// See AdRules doc for more information.
prerollCheckComplete(); } /** * Override the AdRules method. This is called whenever a post-roll * ad can be played. For this example, we want to play both pre-rolls and * post-rolls on every video so execute 'callAdTranslator' here to play an * ad at the post-roll insertion, right after the content finishes playing. */ override public function postrollCheck():void { // callAdTranslator triggers "fetchAd" in ExampleTranslatorWithAdSwf.as
callAdTranslator(); // Let the player know that we are done checking for a post-roll ad.
// See AdRules doc for more information.
postrollCheckComplete(); }
In ExampleTranslatorWithAdSwf.as, the translator tells the player to use the Ad SWF (the one we already initialized in our ad rules class) to render all ads in the player using the function useAdSwf() in the required function override fetchAd(). See the translator class comments for more details and explanation. Basically, this ad translator tells the player to always use the specified Ad SWF to display ads.
If you open up ExampleAdSwfForAdRules.as, you'll see that we override config.pauseVideoPlayer in the class constructor and set it to true. We do this because we want to simulate a linear ad by pausing content playback while the ad renders. Setting this config to true tells the player to act as though this ad is a linear ad.
public function ExampleAdSwfForAdRules():void { config.pauseVideoPlayer = true; }
Also, in ad SWFs, we must override the function displayAd(), as fetchAd() in the translator triggers this function. In ExampleAdSwfForAdRules.as, the ad is rendered when displayAd() is called and we use the advertising API reference to call showSponsorMessage(true) so that the player controls will be disabled during our ad play. Alternatively, you could choose to use allowThirdPartyControl(true) instead of showSponsorMessage(true) if you want to allow the user to be able to interact with your ad via the volume control and play/pause control. You can see descriptions of these various ad modes in the AdvertisingModule API documentation. In ad SWFs, you must also remember to override adComplete() and remove references as needed before the ad play is complete. See the ad SWF class for more details and explanation.
In summary, the basic workflow for a player using the example Ad Rules SWF is the following:

There may be cases where you need to use the default Brightcove ad translator for DFP. This translator is compiled into the players, so you cannot load it in using loadAdTranslator(). If you need to set it as the current ad translator, however, you can use the function useDefaultDFPTranslator(true) in AdRules. Then, you can use the Player API to set the ad server URL on the player ad policy (AdvertisingModule.setAdPolicy(adPolicyObject)). See DFPExample.as and DFPExample_config.xml in AdRulesDevKit.zip for an example of this.