Silverlight MediaElement Video Player ( 可連接 Flash Media Server 4.5 / IIS Media Services 4.1 – MP4 H.264 )

 
Silverlight MediaElement Video Player ( 可連接 Flash Media Server 4.5 / IIS Media Services 4.1 – MP4 H.264 )
 

   App.xaml.cs
 
   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 SilverlightApplication10
   {
      public partial class App : Application
      {
         public App()
         {
            … …
         }
 
         private void Application_Startup(object sender, StartupEventArgs e)
         {
            if (e.InitParams.Count > 2 && e.InitParams["videourl"] != null && e.InitParams["file"] != null && e.InitParams["filetype"] != null)
            {
               this.RootVisual = new MainPage(e.InitParams["videourl"].ToString().Trim(),
               e.InitParams["file"].ToString().Trim(),
               e.InitParams["filetype"].ToString().Trim());
            }
            else
            {
               this.RootVisual = new MainPage("", "", "");
            }
         }
 
         private void Application_Exit(object sender, EventArgs e)
         {
            … …
         }
 
         private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
         {
            … …
         }
 
         private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
         {
            … …
         }
      }
   }
 

 

   MainPage.xaml
 
   <UserControl x:Class="SilverlightApplication10.MainPage"
      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/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      d:DesignHeight="450" d:DesignWidth="600"
      xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
      BorderBrush="#DB33C533" OpacityMask="#FFD65252"
      Background="#E6BD2CE7" Foreground="#E600BBB9">
 
      <Grid x:Name="LayoutRoot" Background="Black">
         <MediaElement Name="mediaElement1" HorizontalAlignment="Left"
         VerticalAlignment="Top" Stretch="Fill" Width="600" Margin="0,25,0,58" />
         <Button Content="Play" Height="23" HorizontalAlignment="Left"
         Margin="1,420,0,0" Name="play_btn" VerticalAlignment="Top" Width="75" Click="play_btn_Click" />
         <Button Content="Stop" Height="23" HorizontalAlignment="Left"
         Margin="77,420,0,0" Name="stop_btn" VerticalAlignment="Top" Width="75" Click="stop_btn_Click" />
         <Button Content="Next" Height="23" HorizontalAlignment="Left"
         Margin="153,420,0,0" Name="next_btn" VerticalAlignment="Top" Width="75" Click="next_btn_Click" />
         <Button Content="Full Screen" Height="23" HorizontalAlignment="Left"
         Margin="524,420,0,0" Name="fullscreen_btn" VerticalAlignment="Top"
         Width="75" Click="fullscreen_btn_Click" />
         <sdk:Label Height="23" HorizontalAlignment="Left" Margin="294,425,0,0"
         Name="player_status" VerticalAlignment="Top" Width="200"
         FontWeight="Normal" FontFamily="Arial" FontSize="13" Foreground="#FFE4BBBB" />
         <Rectangle Height="5" HorizontalAlignment="Left" Margin="0,406,0,0"
         Name="fullprogress" StrokeThickness="1" VerticalAlignment="Top"
         Width="600" Fill="#FF2B2F3E" MouseLeftButtonDown="TimeSlideMove" />
         <Rectangle Height="5" HorizontalAlignment="Left" Margin="0,406,0,0"
         Name="playprogress" StrokeThickness="1" VerticalAlignment="Top"
         Width="0" Fill="#FFCA9BAD" MouseLeftButtonDown="TimeSlideMove" />
      </Grid>
   </UserControl>
 

 

   MainPage.xaml.cs
 
   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;
   using System.Windows.Threading;
 
   namespace SilverlightApplication10
   {
      public partial class MainPage : UserControl
      {
 
         int FileName = 0;
         String URL = null;
         String FileType = null;
 
         public MainPage(String URL, String FileName, String FileType)
         {
            InitializeComponent();
 
            this.Cursor = Cursors.Arrow;
 
            if(URL != null && URL != ""){
               this.URL = URL;
               this.FileName = Int32.Parse(FileName);
               this.FileType = FileType;
               this.mediaElement1.AutoPlay = true;
               this.mediaElement1.Source = new Uri(URL + FileName + FileType);
            }
            this.mediaElement1.MouseLeftButtonDown += new MouseButtonEventHandler(mediaElement1_MouseLeftButtonDown);
 
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
         }
 
         void mediaElement1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
            CheckFullScreen();
         }
 
         private void timer_Tick(object sender, EventArgs e)
         {
            if (this.mediaElement1.CurrentState == MediaElementState.Playing)
            {
               this.player_status.Content =
               this.mediaElement1.Position.Hours.ToString("00") + ":" +
               this.mediaElement1.Position.Minutes.ToString("00") + ":" +
               this.mediaElement1.Position.Seconds.ToString("00") + " / " +
               this.mediaElement1.NaturalDuration.TimeSpan.Hours.ToString("00") + ":" +
               this.mediaElement1.NaturalDuration.TimeSpan.Minutes.ToString("00") + ":" +
               this.mediaElement1.NaturalDuration.TimeSpan.Seconds.ToString("00");
 
               this.playprogress.Width =
               (mediaElement1.Position.TotalMilliseconds / mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds) *
               this.fullprogress.Width;
 
               CheckFullScreen();
            }
 
            if (mediaElement1.Position.TotalMilliseconds > (mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds-1))
            {
               this.mediaElement1.Position = new TimeSpan(0, 0, 0, 0, 0);
               this.mediaElement1.Stop();
 
               this.player_status.Content =
               this.mediaElement1.Position.Hours.ToString("00") + ":" +
               this.mediaElement1.Position.Minutes.ToString("00") + ":" +
               this.mediaElement1.Position.Seconds.ToString("00") + " / " +
               this.mediaElement1.NaturalDuration.TimeSpan.Hours.ToString("00") + ":" +
               this.mediaElement1.NaturalDuration.TimeSpan.Minutes.ToString("00") + ":" +
               this.mediaElement1.NaturalDuration.TimeSpan.Seconds.ToString("00");
 
               this.playprogress.Width =
               (mediaElement1.Position.TotalMilliseconds / mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds) *
               this.fullprogress.Width;
            }
         }
 
         private void play_btn_Click(object sender, RoutedEventArgs e)
         {
            this.mediaElement1.Stop();
            this.mediaElement1.Play();
         }
 
         private void stop_btn_Click(object sender, RoutedEventArgs e)
         {
            this.mediaElement1.Stop();
            timer_Tick(null, null);
         }
 
         private void TimeSlideMove(object sender, MouseButtonEventArgs e)
         {
            this.mediaElement1.Position =
            (new TimeSpan(0, 0, 0, 0,
            (int)(((e.GetPosition(null).X – ((Rectangle)sender).Margin.Left) / this.fullprogress.Width) *
            (this.mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds))));
 
            this.playprogress.Width =
            (mediaElement1.Position.TotalMilliseconds / mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds) *
            this.fullprogress.Width;
            this.mediaElement1.Play();
         }
 
         private void fullscreen_btn_Click(object sender, RoutedEventArgs e)
         {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
            CheckFullScreen();
         }
 
         private void CheckFullScreen()
         {
            if (Application.Current.Host.Content.IsFullScreen)
            {
               this.mediaElement1.Width = Application.Current.Host.Content.ActualWidth;
               this.play_btn.Visibility = System.Windows.Visibility.Collapsed;
               this.stop_btn.Visibility = System.Windows.Visibility.Collapsed;
               this.next_btn.Visibility = System.Windows.Visibility.Collapsed;
               this.fullscreen_btn.Visibility = System.Windows.Visibility.Collapsed;
               this.player_status.Visibility = System.Windows.Visibility.Collapsed;
               this.playprogress.Visibility = System.Windows.Visibility.Collapsed;
               this.fullprogress.Visibility = System.Windows.Visibility.Collapsed;
               this.Cursor = Cursors.None;
            }
            else
            {
               this.mediaElement1.Width = 600;
               this.play_btn.Visibility = System.Windows.Visibility.Visible;
               this.stop_btn.Visibility = System.Windows.Visibility.Visible;
               this.next_btn.Visibility = System.Windows.Visibility.Visible;
               this.fullscreen_btn.Visibility = System.Windows.Visibility.Visible;
               this.player_status.Visibility = System.Windows.Visibility.Visible;
               this.playprogress.Visibility = System.Windows.Visibility.Visible;
               this.fullprogress.Visibility = System.Windows.Visibility.Visible;
               this.Cursor = Cursors.Arrow;
            }
         }
 
         private void next_btn_Click(object sender, RoutedEventArgs e)
         {
            this.FileName++;
            this.mediaElement1.Source = new Uri(URL + (FileName).ToString().Trim() + FileType);
            this.mediaElement1.Play();
         }
      }
   }