`
gstarwd
  • 浏览: 1488353 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

新开发的silverlight视频播放器,

阅读更多

http://www.chenjiliang.com/Article/View.aspx?ArticleID=13574&TypeID=99

再看设计2008-11-22修改为最新版本


 

 

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
             xmlns:d="http://schemas.microsoft.com/expression/blend/2006" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             mc:Ignorable="d"      
             x:Class="Kkun.KkunMovie"     
             Width="600" Height="400" 
             x:Name="GlobeFrame" Foreground="#FF1D1D1D" Background="#FF1D1D1D">
    <UserControl.Resources>
        <Style x:Name="VolumeSliderStyle" TargetType="Rectangle">
            <Setter Property="Fill" Value="#FF414141"></Setter>
            <Setter Property="Stroke" Value="#002A2A29"></Setter>
            <Setter Property="StrokeThickness" Value="0"></Setter>
            <Setter Property="RadiusX" Value="0"></Setter>
            <Setter Property="RadiusY" Value="0"></Setter>
            <Setter Property="HorizontalAlignment" Value="Right"></Setter>
            <Setter Property="VerticalAlignment" Value="Top"></Setter>
            <Setter Property="Height" Value="20"></Setter>
            <Setter Property="Cursor" Value="Hand"></Setter>
        </Style>
    </UserControl.Resources>
    <Grid x:Name="Frame" Width="Auto" Height="Auto">
        <!--FrameIframe-->
        <Rectangle Fill="#FF1D1D1D" Stroke="#FF2A2A29" RadiusX="10" RadiusY="10" Margin="0,0,0,0" x:Name="FrameIframe" d:IsLocked="True" />
        <!--播放区框架-->
        <Grid Margin="10,30,10,11" x:Name="播放区框架">
            <MediaElement x:Name="TstringMovieObject" Margin="2,2,2,31" Source="111.WMV" Canvas.ZIndex="10"/>
            <!--PlayScreen-->
            <Rectangle Stroke="#FF2A2A29" StrokeThickness="1" RadiusX="0" RadiusY="0" Margin="0,0,0,30" x:Name="PlayScreen" Fill="#FF000000" />
            <!--播放状态切换-->
            <TextBlock x:Name="ControlPlay" Cursor="Hand" HorizontalAlignment="Left" Margin="0,0,0,2" VerticalAlignment="Bottom" Width="Auto" Height="Auto" FontSize="16" Foreground="#FFFFFFFF" Text="播放" TextWrapping="Wrap"/>
            <!--音量控制-->
            <Grid HorizontalAlignment="Right" Margin="0,10,10,50" x:Name="VolumeSlider" VerticalAlignment="Center" Width="Auto" Height="Auto" Canvas.ZIndex="100" Background="#00000000">
                <Rectangle x:Name="_1" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,0,0,0" VerticalAlignment="Top" Width="30" Height="21"/>
                <Rectangle x:Name="_2" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,20,0,0" VerticalAlignment="Top" Width="30" Height="21"/>
                <Rectangle x:Name="_3" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,40,0,0" VerticalAlignment="Top" Width="29" Height="21"/>
                <Rectangle x:Name="_4" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,60,0,0" VerticalAlignment="Top" Width="28" Height="21"/>
                <Rectangle x:Name="_5" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,80,0,0" VerticalAlignment="Top" Width="27" Height="21"/>
                <Rectangle x:Name="_6" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,100,0,0" VerticalAlignment="Top" Width="26" Height="21"/>
                <Rectangle x:Name="_7" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,120,0,0" VerticalAlignment="Top" Width="25" Height="21"/>
                <Rectangle x:Name="_8" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,140,0,0" VerticalAlignment="Top" Width="24" Height="21"/>
                <Rectangle x:Name="_9" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,160,0,0" VerticalAlignment="Top" Width="23" Height="21"/>
                <Rectangle x:Name="_10" Fill="#FF414141" Stroke="#002A2A29" StrokeThickness="0" RadiusX="0" RadiusY="0" HorizontalAlignment="Right" Margin="0,180,0,0" VerticalAlignment="Top" Width="22" Height="21"/>
            </Grid>
            <Grid Margin="40,0,0,2" x:Name="SliderProcessBar" VerticalAlignment="Bottom" Height="20" HorizontalAlignment="Stretch" Canvas.ZIndex="9">
                <!--加载进度-->
                <Grid x:Name="DownloadedPercent" Canvas.ZIndex="11" HorizontalAlignment="Left" Width="Auto">
                    <Rectangle x:Name="TstringSliderLoaded" Cursor="Hand" Fill="#FF4F4F4F" Stroke="#00A5A5A5"  StrokeThickness="0" RadiusX="5" RadiusY="5" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" Margin="0,0,0,0" >
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform ScaleX="-1" ScaleY="1"/>
                                <SkewTransform AngleX="0" AngleY="0"/>
                                <RotateTransform Angle="0"/>
                                <TranslateTransform X="0" Y="0"/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                    <TextBlock x:Name="TstringSliderLoadedPercentText" HorizontalAlignment="Right" Margin="0,0,10,0" Width="Auto" FontFamily="Verdana" FontSize="10" FontWeight="Normal" Foreground="#FF9D9D9D" Text="56%" TextWrapping="Wrap" VerticalAlignment="Center" />
                </Grid>
                <!--播放进度-->
                <Rectangle x:Name="TstringPlayState" Cursor="Hand" Canvas.ZIndex="12" Stroke="#00A5A5A5" StrokeThickness="2" RadiusX="5" RadiusY="5" Width="1" HorizontalAlignment="Left">
                    <Rectangle.Resources>
                        <SolidColorBrush x:Key="BrushVistaStyle" Color="#FF3D3838"/>
                    </Rectangle.Resources>
                    <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="0.524,0.498" StartPoint="0.524,0.502">
                            <GradientStop Color="#FF313131" Offset="0.0"/>
                            <GradientStop Color="#FF414141" Offset="1"/>
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                </Rectangle>
                <!--播放进度背景-->
                <Rectangle x:Name="TstringSliderBg" Cursor="Hand" Canvas.ZIndex="10" Fill="#FF767676" Stroke="#FF1A1A1A" StrokeThickness="0" RadiusX="5" RadiusY="5" OpacityMask="#FF313131" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" Margin="0,0,0,0">
                    <Rectangle.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="-1" ScaleY="1"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform Angle="0"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Rectangle.RenderTransform>
                </Rectangle>
            </Grid>
        </Grid>
        <!--右上角装饰方框-->
        <Grid Margin="0,8,20,0" x:Name="右上角装饰方框" VerticalAlignment="Top" Height="15" HorizontalAlignment="Right" Width="Auto" d:IsLocked="True">
            <!--影片合适大小-->
            <Rectangle x:Name="tPlayButton" ToolTipService.ToolTip="播放" Cursor="Hand" Fill="#FF5A5A5A" Stroke="#FF2A2A29" StrokeThickness="0" RadiusX="2" RadiusY="2" Width="15" HorizontalAlignment="Left" Margin="0,0,0,0"/>
            <!--影片合适大小-->
            <Rectangle x:Name="tFullScreenButton" ToolTipService.ToolTip="全屏" Cursor="Hand" Fill="#FF5A5A5A" Stroke="#FF2A2A29" StrokeThickness="0" RadiusX="2" RadiusY="2" Margin="17,0,0,0" HorizontalAlignment="Left" Width="15"/>
            <!--静音-->
            <Rectangle x:Name="tMutedButton" ToolTipService.ToolTip="静音" Cursor="Hand" Fill="#FF5A5A5A" Stroke="#FF2A2A29" StrokeThickness="0" RadiusX="2" RadiusY="2" Width="15" HorizontalAlignment="Left" Margin="34,0,0,0"/>
            <!--循环播放-->
            <Rectangle x:Name="tLoop" ToolTipService.ToolTip="循环" Cursor="Hand" Fill="#FF5A5A5A" Stroke="#FF2A2A29" StrokeThickness="0" RadiusX="2" RadiusY="2" Width="15" HorizontalAlignment="Left" Margin="51,0,0,0"/>
        </Grid>
        <!--网址-->
        <TextBlock Cursor="Hand" HorizontalAlignment="Left" Margin="10,10,0,0" x:Name="网址" VerticalAlignment="Top" Width="Auto" Height="Auto" Foreground="#FFAAAAAA" Text="tstring.com.cn" TextWrapping="Wrap" d:IsLocked="True"/>
        <!--提示信息-->
        <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" x:Name="tbInfo" VerticalAlignment="Center" FontSize="20" FontWeight="Bold" Text="缓冲" TextWrapping="Wrap"/>
    </Grid>
</UserControl>

 

 

最后源码2008-11-22修改为最新版本

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Kkun
{
    public partial class KkunMovie : UserControl
    {
        public bool IsLeftButtonPressed = false;
        public bool IsLoop = false;

        public KkunMovie()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(TstringMovieControl_Loaded);
        }
        void TstringMovieControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeEvent();
        }

        private void InitializeEvent()
        {
            tbInfo.Text = string.Empty;
            VolumeSlider.Opacity = 0;
            VolumeSlider.MouseEnter += new MouseEventHandler(VolumeSlider_MouseEnter);
            VolumeSlider.MouseLeave += new MouseEventHandler(VolumeSlider_MouseLeave);

            TstringMovieObject.MediaOpened += new RoutedEventHandler(TstringMovieObject_MediaOpened);
            TstringMovieObject.DownloadProgressChanged += new RoutedEventHandler(TstringMovieObject_DownloadProgressChanged);
            TstringMovieObject.MarkerReached += new TimelineMarkerRoutedEventHandler(TstringMovieObject_MarkerReached);
            TstringMovieObject.MediaEnded += new RoutedEventHandler(TstringMovieObject_MediaEnded);
            TstringMovieObject.CurrentStateChanged += new RoutedEventHandler(TstringMovieObject_CurrentStateChanged);


            ControlPlay.MouseLeftButtonDown += new MouseButtonEventHandler(ControlPlay_MouseLeftButtonDown);
            tMutedButton.MouseLeftButtonDown += new MouseButtonEventHandler(tMutedButton_MouseLeftButtonDown);
            tPlayButton.MouseLeftButtonDown += new MouseButtonEventHandler(ControlPlay_MouseLeftButtonDown);
            tFullScreenButton.MouseLeftButtonDown += new MouseButtonEventHandler(tFullScreenButton_MouseLeftButtonDown);
            tLoop.MouseLeftButtonDown += new MouseButtonEventHandler(tLoop_MouseLeftButtonDown);

            TstringPlayState.MouseLeftButtonDown += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonDown);
            TstringPlayState.MouseMove += new MouseEventHandler(TstringPlayState_MouseMove);
            TstringPlayState.MouseLeftButtonUp += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonUp);

            DownloadedPercent.MouseLeftButtonDown += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonDown);
            DownloadedPercent.MouseMove += new MouseEventHandler(TstringPlayState_MouseMove);
            DownloadedPercent.MouseLeftButtonUp += new MouseButtonEventHandler(TstringPlayState_MouseLeftButtonUp);

            #region

            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);

            for (int i = 1; i <= 20; i++)
            {
                Rectangle VolumeItem = (Rectangle) this.FindName(string.Format("_{0}", i));
                if (VolumeItem != null)
                {
                    VolumeItem.MouseEnter += new MouseEventHandler(VolumeItem_MouseEnter);
                    VolumeItem.MouseLeave += new MouseEventHandler(VolumeItem_MouseEnter);
                }
            }
            #endregion

        }

        void TstringMovieObject_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            switch (this.TstringMovieObject.CurrentState)
            {
            case MediaElementState.Buffering:
            this.Info("Buffering");
            break;
            case MediaElementState.Closed:
            this.Info("Closed");
            break;
            case MediaElementState.Opening:
            this.Info("Opening");
            break;
            case MediaElementState.Paused:
            this.Info("Paused");
            break;
            case MediaElementState.Playing:
            this.Info(null);
            break;
            case MediaElementState.Stopped:
            this.Info("Stopped");
            break;
            default:
            this.Info(null);
            break;
            }
        }

        void tLoop_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsLoop = !IsLoop;
            tLoop.Opacity = IsLoop ? 0.5 : 1;
        }

        void TstringMovieObject_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (IsLoop)
            {
                ControlPlay_MouseLeftButtonDown(sender, null);
            }
            tPlayButton.Opacity = 1;
            ControlPlay.Text = "重播";
            TstringPlayState.Width = 0;
            DownloadedPercent.Width = OffsetLoadedProcessBarLeng();
            TstringMovieObject.Position = new TimeSpan(0, 0, 0);
            TstringSliderLoadedPercentText.Text = string.Empty;
        }

        void TstringMovieObject_DownloadProgressChanged(object sender, RoutedEventArgs e)
        {
            DownloadedPercent.Width = TstringMovieObject.DownloadProgress * OffsetLoadedProcessBarLeng();
            TstringSliderLoadedPercentText.Text = string.Format("{0}%", (int) (TstringMovieObject.DownloadProgress * 100));
        }

        void TstringSliderBg_MouseLeave(object sender, MouseEventArgs e)
        {
            IsLeftButtonPressed = false;
        }

        void TstringPlayState_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsLeftButtonPressed = false;
        }

        void TstringPlayState_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsLeftButtonPressed)
            {
                double seconds = this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;

                TstringPlayState.Width = e.GetPosition((UIElement) sender).X;
                TstringMovieObject.Position = new TimeSpan(0, 0, (int) (seconds / OffsetLeng() * TstringPlayState.Width));
            }
        }

        void TstringPlayState_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsLeftButtonPressed)
            {
                IsLeftButtonPressed = true;
                double seconds = this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;

                TstringPlayState.Width = e.GetPosition((UIElement) sender).X;
                TstringMovieObject.Position = new TimeSpan(0, 0, (int) (seconds / OffsetLeng() * TstringPlayState.Width));
            }
        }

        void tFullScreenButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
            FrameIframe.Visibility = Application.Current.Host.Content.IsFullScreen ? Visibility.Collapsed : Visibility.Visible;
            tFullScreenButton.Opacity = Application.Current.Host.Content.IsFullScreen ? 0.5 : 1;
        }

        void tMutedButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TstringMovieObject.IsMuted = !TstringMovieObject.IsMuted;
            tMutedButton.Opacity = TstringMovieObject.IsMuted ? 0.5 : 1;
        }

        void VolumeSlider_MouseLeave(object sender, MouseEventArgs e)
        {
            VolumeSlider.Opacity = 0;
        }

        void VolumeSlider_MouseEnter(object sender, MouseEventArgs e)
        {
            VolumeSlider.Opacity = 1;
        }

        void Content_FullScreenChanged(object sender, EventArgs e)
        {

            if (Application.Current.Host.Content.IsFullScreen)
            {
                GlobeFrame.Width = Application.Current.Host.Content.ActualWidth;
                GlobeFrame.Height = Application.Current.Host.Content.ActualHeight;
            }
            else
            {
                GlobeFrame.Width = 600;
                GlobeFrame.Height = 400;
            }
            DownloadedPercent.Width = this.Width - 60;
        }

        void VolumeItem_MouseEnter(object sender, MouseEventArgs e)
        {
            Rectangle VolumeItem = e.OriginalSource as Rectangle;
            if (VolumeItem != null)
            {
                double _CurrentIndex = double.Parse(VolumeItem.Name.Replace("_", ""));
                for (int i = 1; i <= 20; i++)
                {
                    Rectangle _TempRectangle = (Rectangle) this.FindName("_" + i);
                    if (_TempRectangle != null)
                    {
                        if (i >= _CurrentIndex)
                        {
                            _TempRectangle.Opacity = 1;
                        }
                        else
                        {
                            _TempRectangle.Opacity = 0.2;
                        }
                    }
                }

                this.TstringMovieObject.Volume = 1 - _CurrentIndex / 10;
            }
        }

        void TstringMovieObject_MarkerReached(object sender, TimelineMarkerRoutedEventArgs e)
        {
            if (this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds > 0)
            {
                TstringPlayState.Width = e.Marker.Time.TotalSeconds * OffsetLeng() / this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;
            }
        }

        void TstringMovieObject_MediaOpened(object sender, RoutedEventArgs e)
        {
            double seconds = this.TstringMovieObject.NaturalDuration.TimeSpan.TotalSeconds;
            for (int i = 1; i < OffsetLeng(); i++)
            {
                TimelineMarker marker = new TimelineMarker();
                double time = seconds / OffsetLeng() * i;
                marker.Time = new TimeSpan(0, 0, (int) time);
                marker.Text = time.ToString();
                marker.Type = "Slider";
                this.TstringMovieObject.Markers.Add(marker);
            }
        }

        void ControlPlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            switch (this.TstringMovieObject.CurrentState)
            {
            case MediaElementState.Paused:
            this.TstringMovieObject.Play();
            tPlayButton.Opacity = 1;
            ControlPlay.Text = "播放";
            this.Info(string.Empty);
            break;
            case MediaElementState.Playing:
            this.TstringMovieObject.Pause();
            tPlayButton.Opacity = 0.5;
            ControlPlay.Text = "暂停";
            this.Info("暂停");
            break;
            case MediaElementState.Stopped:
            this.TstringMovieObject.Pause();
            tPlayButton.Opacity = 1;
            ControlPlay.Text = "播放";
            break;
            default:
            break;
            }
        }

        #region Method

        private double OffsetLoadedProcessBarLeng()
        {
            return this.Width - 60;
        }

        private double OffsetLeng()
        {
            return this.Width - 140;
        }

        private void Info(string Information)
        {
            if (string.IsNullOrEmpty(Information))
            {
                tbInfo.Text = string.Empty;
            }
            else
            {
                tbInfo.Text = Information;
            }
        }

        #endregion

    }
}
 


说明

下载:http://files.cnblogs.com/kkun/KkunMovie.rar
开发工具:Microsoft Expression Blend 2,Microsoft Visual Studio 2008,c# 

布局基本上都是相对位置,设置总长宽则所有控件均按比例绽放
下载完成后唯一要做的就是在TestPage页面目录下添加一个111.wmv格式的视频文件,这个属性在xaml的26行

如何卸载旧的SL客户端,TOOLS,SDK?请跟我来
http://forums.microsoft.com/china/ShowPost.aspx?PostID=4120391&SiteID=15

 

  • 大小: 34.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics