private void StreamSelectionManager_StreamSelectionCompleted(StreamSelectionManager streamSelectionManager, Segment segment, IEnumerable<MediaStream> streams, StreamUpdatedListEventArgs e)
 {
     if (e.Error == null)
     {
         foreach (StreamUpdatedEventArgs update in e.StreamUpdatedEvents)
         {
             if (update.Action == StreamUpdatedEventArgs.StreamUpdatedAction.StreamSelected)
             {
                 StreamSelected.IfNotNull(i => i(this, new MediaStream(update.Stream)));
             }
             else if (update.Action == StreamUpdatedEventArgs.StreamUpdatedAction.StreamDeselected)
             {
                 StreamUnselected.IfNotNull(i => i(this, new MediaStream(update.Stream)));
             }
         }
     }
     else
     {
         StreamSelectionFailed.IfNotNull(i => i(this, streams.Cast<IMediaStream>(), e.Error));
     }
 }
        private void UnloadManifest()
        {
#if !WINDOWS_PHONE
            if (MediaElement != null && MediaElement.ManifestInfo != null)
            {
                MediaElement.ManifestInfo.ChunkListChanged -= ManifestInfo_ChunkListChanged;
            }
#endif
            if (_streamSelectionManager != null)
            {
                _streamSelectionManager.StreamSelectionCompleted -= StreamSelectionManager_StreamSelectionCompleted;
                _streamSelectionManager.StreamSelectionExceededMaximumRetries -= StreamSelectionManager_StreamSelectionExceededMaximumRetries;
                _streamSelectionManager.Dispose();
                _streamSelectionManager = null;
            }
        }
 private void StreamSelectionManager_StreamSelectionExceededMaximumRetries(StreamSelectionManager streamSelectionManager, Segment segment, IEnumerable<MediaStream> streams)
 {
     StreamSelectionFailed.IfNotNull(i => i(this, streams.Cast<IMediaStream>(), new StreamSelectionTimeOutException()));
 }
        private void MediaElement_ManifestReady(object sender, EventArgs e)
        {
            UnloadManifest();

#if !WINDOWS_PHONE
            MediaElement.ManifestInfo.ChunkListChanged += ManifestInfo_ChunkListChanged;
#endif

            _streamSelectionManager = new StreamSelectionManager(MediaElement.ManifestInfo);
            _streamSelectionManager.StreamSelectionCompleted += StreamSelectionManager_StreamSelectionCompleted;
            _streamSelectionManager.StreamSelectionExceededMaximumRetries += StreamSelectionManager_StreamSelectionExceededMaximumRetries;
#if !WINDOWS_PHONE
            _scheduledAds.Where(i => !i.IsScheduled)
                .ForEach(ScheduleAd);
            _scheduledAds.Clear();
#endif
            ManifestReady.IfNotNull(i => i(this));
        }