Pausing Video when a Companion Ad is Clicked

When building a media app on Windows Phone 8.1, it is very popular for developers to use the Microsoft Media Platform Player Framework, and open source media player framework for both Windows and Windows Phone.   The framework is made up of a number of components which enable developers to include just the component that they need for their media scenario.  One of these components is for pre-roll, mid-roll, and post-roll advertisements – which, when connected to an ad service like FreeWheel, will display video ads at certain times during playback.  One of the options for the advertising component is to display image-based companion ads while a video ad is playing back.  This image can be clicked on to bring a viewer to a web-based landing page for the advertisement.

Working with a partner, we found out that the typical Xaml page navigation events (Page.OnNavigatedTo and Page.OnNavigatedFrom) don’t fire immediately when the user clicks on one of the companion ads on Windows.  This can be problematic if you need to pause the video playback when the user clicks on the ad.  In this case, you need to handle the CoreWindow.VisibilityChanged event.  Attach this event on Page.OnNavigatedTo and detach it on the Page.OnNavigatedFrom. 

 protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    this.navigationHelper.OnNavigatedTo(e);

    CoreWindow.GetForCurrentThread().VisibilityChanged += 
         this.OnVisibilityChanged;
}
  
 protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);

    CoreWindow.GetForCurrentThread().VisibilityChanged –= 
         this.OnVisibilityChanged;
}

At that point, the code that you need to call would be something like this:

 private void OnVisibilityChanged(
     CoreWindow sender, 
     VisibilityChangedEventArgs args)
{
    if (args.Visible)
    {
        this.Player.InteractiveViewModel.PlayResume();
    }
    else
    {
        this.Player.InteractiveViewModel.Pause();
    }
}

 

As the Player Framework is Open Source, the app developer community is encouraged to download the source code, fork it, and suggestion fixes and enhancements.  The latest source code in the player framework is in the “universal” branch.