This project is read-only.

Scheduling Advertisements in code

Topics: Windows 8 Xaml
Aug 7, 2012 at 10:54 PM

The samples are very clear as to how to schedule VAST advertisements in the XAML, but it is not clear to me how to pass a valid VAST URL to the player control in order to schedule the playback of an ad.  In the Silverlight player framework, there is a method:

player.ScheduleAdTrigger("valid VAST URI");

I am unable to find anything similar in the Microsoft Metro Media Player Framework branch that indicates how to programmatically access the Advertising plugin.  Any ideas?

Aug 8, 2012 at 5:05 PM

HipCzeck, to schedule an ad from code, you need to add a MidrollAdvertisement object AdSchedulerPlugin.Advertisements collection.

For example:

var adScheduler = player.GetAdSchedulerPlugin();
var myMidroll = new MidrollAdvertisement();
myMidroll.Time = TimeSpan.FromSeconds(20);
myMidroll.Source = new RemoteAdSource(new Uri(""), VideoAdvertising.VastAdPayloadHandler.AdType);

Note: This assumes the AdSchedulerPlugin is already added to your plugins collection. IF not, you can add it in Xaml or via: player.Plugins.Add(new AdSchedulerPlugin()))



Sep 21, 2012 at 5:04 PM

Thanks.  This has been most helpful.  I'm now writing a custom AdHandlerPlugin based upon the example in the MMPPF.  I'm wondering when in the lifetime of the Player control it is appropriate to register AdHandlerPlugin and AdSchedulerPlugin using the 

player.Plugins.Add(new AdSchedulerPlugin()))

code you mentioned in your previous response.

If I try to register the plugins too early in the player lifetime, the registration of the plugin elements fails and ads are not scheduled.

Is it appropriate to associate the loading of plugins with the MediaLoading Event returned by the player, or is there another event that is more appropriate?

Thanks again,

Sep 21, 2012 at 5:21 PM

Hi Rob, you should register the AdScheduler plugin anytime before MediaStarting since it uses MediaStarting in order to play a pre-roll.

But, you should be able to register it in the constructor of your page. No time is too early. What is the issue you are running into when registering it early on?

Sep 21, 2012 at 5:48 PM

I'm writing a library that acts as an intermediary between the page and another library that is wrapping the player control, so lifetime is a bit tricky, and sometimes my mediaplayer reference is still null, so I was trying to attach my initialization code to a MediaPlayer context event to ensure it is always properly enabled.  I suppose I can create an Advertising initialization method in my library which is called by the page constructor.