async Task PlayAdsOfType <T>(CancellationToken cancellationToken) where T : Advertisement
        {
            var adsToPlay = EligableAds.OfType <T>().Where(a => a.Source != null).ToList();

            HandledAds.AddRange(adsToPlay.Cast <Advertisement>());
            if (adsToPlay.Any())
            {
                await PlayAds(adsToPlay.Cast <Advertisement>(), cancellationToken);
            }
        }
        async void PreloadAdMarker(TimelineMarker marker)
        {
            var ad = EligableAds.OfType <Advertisement>().Where(a => a.Source != null).FirstOrDefault(a => a.Id == marker.Text);

            if (ad != null)
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var task = MediaPlayer.PreloadAd(ad.Source, cancellationTokenSource.Token);
                if (task != null)
                {
                    await SetOrReplaceActivePreloadOperation(new PreloadOperation(ad.Source, task, cancellationTokenSource));
                }
            }
        }
 async void MediaPlayer_MediaEnding(object sender, MediaPlayerDeferrableEventArgs e)
 {
     if (EligableAds.OfType <PostrollAdvertisement>().Any(a => a.Source != null))
     {
         var deferral = e.DeferrableOperation.GetDeferral();
         try
         {
             await PlayAdsOfType <PostrollAdvertisement>(CancellationTokenSource.CreateLinkedTokenSource(deferral.CancellationToken, cts.Token).Token);
         }
         catch { /* ignore */ }
         finally
         {
             deferral.Complete();
         }
     }
 }
 /// <summary>
 /// Evaluates all markers in a window and plays an ad if applicable.
 /// </summary>
 /// <param name="originalPosition">The window start position.</param>
 /// <param name="newPosition">The window end position. Note: This can be before originalPosition if going backwards.</param>
 /// <returns>A boolean indicating that an ad was played.</returns>
 public bool EvaluateMarkers(TimeSpan originalPosition, TimeSpan newPosition)
 {
     if (!EvaluateOnForwardOnly || newPosition > originalPosition)
     {
         foreach (var marker in MediaPlayer.Markers.Where(m => m.Type == MarkerType_Play).ToList())
         {
             if (marker.Time <= newPosition && marker.Time > originalPosition)
             {
                 var ad = EligableAds.OfType <MidrollAdvertisement>().Where(a => a.Source != null).FirstOrDefault(a => a.Id == marker.Text);
                 if (ad != null)
                 {
                     var syncToPosition = SeekToAdPosition ? marker.Time.Add(ad.Duration) : newPosition;
                     PlayAd(ad, syncToPosition);
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
        internal async Task PlayStartupAds(CancellationToken c)
        {
            if (MediaPlayer.AllowMediaStartingDeferrals)
            {
                IList <Advertisement> startupAds;
                if (MediaPlayer.StartupPosition.HasValue)
                {
                    startupAds = EligableAds
                                 .OfType <MidrollAdvertisement>()
                                 .Where(a => a.Source != null && a.Time == MediaPlayer.StartupPosition.Value)
                                 .Cast <Advertisement>()
                                 .ToList();
                }
                else
                {
                    startupAds = EligableAds
                                 .OfType <PrerollAdvertisement>()
                                 .Where(a => a.Source != null)
                                 .Cast <Advertisement>()
                                 .ToList();
                }


                if (startupAds.Any())
                {
                    try
                    {
                        using (var adCts = CancellationTokenSource.CreateLinkedTokenSource(c, cts.Token))
                        {
                            HandledAds.AddRange(startupAds);
                            await PlayAds(startupAds, adCts.Token);
                        }
                    }
                    catch { /* ignore */ }
                }
            }
        }