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]);
        }

转载于:https://www.cnblogs.com/allanli/archive/2010/08/06/1793741.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值