Silverlight MediaElement 设定Source后停止自动下载

通常使用MediaElement设定Source并把它加到对应的Layout中,MediaElement就会自动连接远程流媒体并自动下载。下面的方法可以只获得一部分流,之后停止下载。

        Dictionary<MediaElement, Storyboard> mediaElementList = new Dictionary<MediaElement, Storyboard>();
        Dictionary<Storyboard, MediaElement> storyBoadList = new Dictionary<Storyboard, MediaElement>();
        Dictionary<Storyboard, MediaInfo> storyBoadMediaInfos = new Dictionary<Storyboard, MediaInfo>();

        public void LoadMedias()
        {
            List<MediaInfo> mediaInfoList = MediaAddressList.GetMediaItemList();
            foreach (MediaInfo item in mediaInfoList)
            {
                MediaElement element = new MediaElement();
                element.Source = new Uri(item.Source[0]);
                element.AutoPlay = false;
                element.BufferingTime = new TimeSpan(0, 0, 0, 0, 500);
                element.MediaOpened += new RoutedEventHandler(element_MediaOpened);
                Storyboard storyboard = new Storyboard();
                storyboard.Duration = new Duration(TimeSpan.FromSeconds(0.5));
                storyboard.Completed += new EventHandler(timer_Completed);
                mediaElementList.Add(element, storyboard);
                storyBoadList.Add(storyboard, element);
                storyBoadMediaInfos.Add(storyboard, item);
                this.MediaElementPanel.Children.Add(element);
            }
        }

        private void RemoveElement(MediaElement mediaElement)
        {
            this.MediaElementPanel.Children.Remove(mediaElement);
        }

        private void element_MediaOpened(object sender, RoutedEventArgs e)
        {
            mediaElementList[sender as MediaElement].Begin();
        }

        private void timer_Completed(object sender, EventArgs e)
        {
            WriteableBitmap bitmap = new WriteableBitmap(storyBoadList[sender as Storyboard] as MediaElement, new TranslateTransform());
            MediaClipView clip = new MediaClipView();
            MediaInfo mediaInfo = storyBoadMediaInfos[sender as Storyboard];
            Image image = clip.mediaImage;
            image.Width = 320;
            image.Height = 180;
            image.Margin = new Thickness(5);
            image.Source = bitmap;
            image.DataContext = mediaInfo;
            clip.mediaLabel.Text = mediaInfo.MediaLabel;
            this.ThumbnailsPanel.Children.Add(clip);
            RemoveElement(storyBoadList[sender as Storyboard]);
        }

你可能感兴趣的:(silverlight)