How to Databind RemoteAdSource uri?

Topics: Windows 8 Xaml
Nov 28, 2012 at 3:03 PM

I made an attempt to data bind the Uri of a PreRollAdvertisment as below:

ads:RemoteAdSource Uri="{Binding PreRoll}" Type="vast"



however the xaml parser throws an error: WinRT information: Failed to assign to property 'Microsoft.PlayerFramework.Advertising.RemoteAdSource.Uri'.



If binding is not supported are there any guidance on how to achieve this from an MVVM perspective?





Nov 28, 2012 at 4:15 PM

I have had a go at binding the whole player as the content on a contentcontrol and creating the player in the ViewModel code, this not production just a simple test to see if I can get everything working in a quick and dirty fashion.  The Video plays ok, but none of the Adds play.  following is the code so far:

MainPage.XAML:

<Page
    x:Class="VideoPlayer.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:VideoPlayer"
    xmlns:mmppf="using:Microsoft.PlayerFramework"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ads="using:Microsoft.PlayerFramework.Advertising"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ContentControl  Content="{Binding Player}"/>
    </Grid>
</Page>

And code behind:

 

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.ViewModel = new VideoPlayerViewModel();
            this.InitializeComponent();
        }

        public VideoPlayerViewModel ViewModel
        {
            get { return DataContext as VideoPlayerViewModel; }
            set
            {
                DataContext = value;
            }
        }
     }

VideoPlayerViewModel:

public class VideoPlayerViewModel : ViewModelBase
    {

        private MediaPlayer _player;

        public MediaPlayer Player
        {
            get { return _player; }
            set
            {
                _player = value;
                RaisePropertyChanged(() => this.Player);
            }
        }

        public VideoPlayerViewModel()
        {
            SetVideoPlayer("http://smf.blob.core.windows.net/samples/videos/bigbuck.mp4");
        }

        private void SetVideoPlayer(string url)
        {
            MediaPlayer player = new MediaPlayer();
            var adScheduler = new AdSchedulerPlugin();
            var preRoll = new PrerollAdvertisement();
            preRoll.Source = new RemoteAdSource(new Uri("http://smf.blob.core.windows.net/samples/win8/ads/vast_linear.xml"), Microsoft.VideoAdvertising.VastAdPayloadHandler.AdType);
            adScheduler.Advertisements.Add(preRoll);

            var midRoll = new MidrollAdvertisement();
            midRoll.Time = TimeSpan.FromSeconds(10);
            midRoll.Source = new RemoteAdSource(new Uri("http://smf.blob.core.windows.net/samples/win8/ads/vast_linear.xml"), Microsoft.VideoAdvertising.VastAdPayloadHandler.AdType);
            adScheduler.Advertisements.Add(midRoll);

            var postRoll = new PostrollAdvertisement();
            postRoll.Source = new RemoteAdSource(new Uri("http://smf.blob.core.windows.net/samples/win8/ads/vast_linear.xml"), Microsoft.VideoAdvertising.VastAdPayloadHandler.AdType);
            adScheduler.Advertisements.Add(postRoll);
            player.Plugins.Add(adScheduler);
            player.Source = new Uri(url);
            Player = player;
            player.Play();
        }
}

 

What else is required to make the Ads play?

Coordinator
Nov 28, 2012 at 4:40 PM

Re your first quesiton: you can only bind to dependency properties and the RemoteAdSource.Uri property is not a dependencyproperty. I'll look into changing this in the future to make it more binding friendly. But, one thing you can do is make a copy of the RemoteAdSource class in your project, modify it to your liking and use it instead. As long as it implements IResolvableAdSource it will work.

Re your second question: you are missing the AdHandlerPlugin. Create an instance and add it to the MediaPlayer.Plugins collection and ads should work.

Tim

Nov 29, 2012 at 4:39 PM

Thanks Tim.

I have one other observation. most times when I run the above code, Visual studio Just-In-Time debugger fires up stating: an unhandled win32 exception occurred in videoPlayer.exe [3164]

I did an experiment calling PreLoadAd to see if this would clear the issue, but sadly not. 

Do you have any advice on how I should handle this?

John.

Nov 29, 2012 at 5:27 PM

I think this may be related to the above: when debugging on the SurfaceRT I get lots of the following errors: Adverts and Main Video still play however.

An exception of type 'System.NullReferenceException' occurred in Microsoft.VideoAdvertising.winmd and wasn't handled before a managed/native boundary

An exception of type 'System.NullReferenceException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary

An exception of type 'System.NullReferenceException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary

An exception of type 'System.NullReferenceException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary

An exception of type 'System.OperationCanceledException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary

An exception of type 'System.OperationCanceledException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary

An exception of type 'System.OperationCanceledException' occurred in Microsoft.VideoAdvertising.winmd and wasn't handled before a managed/native boundary

An exception of type 'System.OperationCanceledException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary