Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Silverlight Media Player

, 6 Apr 2009
Has some cool features like sliding menus with animation and Drag N Drop from menu
SilverLightMediaPlayer.zip
SilverLightMediaPlayer
MediaPlayer
Bin
Debug
de
System.Xml.Linq.resources.dll
es
System.Xml.Linq.resources.dll
fr
System.Xml.Linq.resources.dll
it
System.Xml.Linq.resources.dll
ja
System.Xml.Linq.resources.dll
ko
System.Xml.Linq.resources.dll
MediaPlayer.dll
MediaPlayer.xap
System.Xml.Linq.dll
zh-Hans
System.Xml.Linq.resources.dll
zh-Hant
System.Xml.Linq.resources.dll
MediaPlayer.csproj.user
Properties
v1.jpg
v2.jpg
MediaPlayerLive
App_Data
bin
de
System.Web.Silverlight.resources.dll
es
System.Web.Silverlight.resources.dll
fr
System.Web.Silverlight.resources.dll
it
System.Web.Silverlight.resources.dll
ja
System.Web.Silverlight.resources.dll
ko
System.Web.Silverlight.resources.dll
MediaPlayerLive.dll
MediaPlayerLive.pdb
System.Web.Silverlight.dll
zh-Hans
System.Web.Silverlight.resources.dll
zh-Hant
System.Web.Silverlight.resources.dll
ClientBin
MediaPlayer.xap
MediaPlayerLive.csproj.user
Properties
SilverLightMediaPlayer.suo
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace MediaPlayer
{
    public partial class Page : UserControl
    {
        DispatcherTimer timer;
        double volumeValue;
        private double _oldWidth;
        private double _oldHeight;
        public double uniformScaleAmount = 1;

        private String _lastClickedItem = String.Empty;
        private DateTime _lastClickedTime = DateTime.Now;
        private double _doubleClickIntervalTimeInMillisecond = 500d;  

        Image img = null;
        bool isMouseCaptured;
        double mouseVerticalPosition;
        double mouseHorizontalPosition;
     
        private bool IsSliderLocked { get; set; }
        public Page()
        {
            InitializeComponent();
            myPlayer.Play();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(50);
            timer.Tick += new EventHandler(timer_Tick);
            _oldHeight = this.Height;
            _oldWidth = this.Width;
            sliderVolume.Value = myPlayer.Volume;
            ucHGallery.HGalleryInit();
            DataAcces.SetRightVertPanel(vertGallery);

            stackMenu.SetValue(Canvas.TopProperty, myPlayer.Height - stackMenu.Height);
            //Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);
            spPlayer.MouseEnter += new System.Windows.Input.MouseEventHandler(myPlayer_MouseEnter);
            spPlayer.MouseLeave += new System.Windows.Input.MouseEventHandler(myPlayer_MouseLeave);
        }

        void myPlayer_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (stackMenu.Opacity > 0)
            {
                menuBoardHide.Begin();
            }
        }

        private void myPlayer_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (Application.Current.Host.Content.IsFullScreen)
            {
                menuBoardShow.Begin();
            }
        }

        void Content_Resized(object sender, EventArgs e)
        {
            double currentWidth = Application.Current.Host.Content.ActualWidth;
            double currentHeight = Application.Current.Host.Content.ActualHeight;

            if (Application.Current.Host.Content.IsFullScreen)
            {
                uniformScaleAmount = Math.Min((currentWidth / _oldWidth), (currentHeight / _oldHeight));
                spCollapsableMenu.Visibility = Visibility.Collapsed;
                menuBoardHide.Begin();
            }
            else
            {
                uniformScaleAmount = 1;
                spCollapsableMenu.Visibility = Visibility.Visible;
            }
            playerTransform.ScaleX = uniformScaleAmount;
            playerTransform.ScaleY = uniformScaleAmount;            
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (myPlayer.NaturalDuration.TimeSpan.TotalSeconds > 0 && !IsSliderLocked)
            {
                txtDuration.Text = string.Format("{0:00}:{1:00}", myPlayer.Position.Minutes,
                    myPlayer.Position.Seconds);

                sliderPosition.Value = myPlayer.Position.TotalSeconds /
                    myPlayer.NaturalDuration.TimeSpan.TotalSeconds;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (btnPP.Content.ToString() == "ll")
            {
                btnPP.Content = ">";
                myPlayer.Pause();
            }
            else
            {
                btnPP.Content = "ll";
                myPlayer.Position = TimeSpan.FromSeconds(sliderPosition.Value *
                myPlayer.NaturalDuration.TimeSpan.TotalSeconds);
                myPlayer.Play();
            }
        }

        private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            myPlayer.Volume = e.NewValue;
        }

        private void myPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (myPlayer.CurrentState == System.Windows.Media.MediaElementState.Playing)
            {
                timer.Start();
                btnPP.Content = "ll";
            }
            else
            {
                timer.Stop();
                btnPP.Content = ">";
                if (myPlayer.Position == myPlayer.NaturalDuration.TimeSpan)
                {
                    sliderPosition.Value = 0;
                    myPlayer.Stop();
                    btnPP.Content = ">";
                }
            }
        }

        private void sliderPosition_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            IsSliderLocked = true;
        }

        private void sliderPosition_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            IsSliderLocked = false;
            myPlayer.Position = TimeSpan.FromSeconds(sliderPosition.Value *
                myPlayer.NaturalDuration.TimeSpan.TotalSeconds);

        }

        private void btnMute_Click(object sender, RoutedEventArgs e)
        {
            if (btnMute.Content.ToString() == "On")
            {
                volumeValue = sliderVolume.Value;
                myPlayer.Volume = 0;
                btnMute.Content = "Off";
            }
            else
            {
                myPlayer.Volume = volumeValue;
                btnMute.Content = "On";
            }
        }

        private void btnFullScreen_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }

        private void myPlayer_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DateTime currentTime = DateTime.Now;

            if (sender is MediaElement)
            {
                MediaElement item = sender as MediaElement;
                String currentClickedItem = item.Name;

                double intervalBetweenTwoClicks = currentTime.Subtract(_lastClickedTime).TotalMilliseconds;                
                if (intervalBetweenTwoClicks < _doubleClickIntervalTimeInMillisecond
                    && intervalBetweenTwoClicks > 0
                    && _lastClickedItem == currentClickedItem)
                {                   
                    Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
                }

                _lastClickedItem = item.Name;
                _lastClickedTime = DateTime.Now;
            }
        }

        private void StackPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.OriginalSource is Image)
            {
                Image image = (Image)e.OriginalSource;
                img = image;
                popup1.VerticalOffset = e.GetPosition(this).Y - e.GetPosition(image).Y;
                popup1.HorizontalOffset = e.GetPosition(this).X - e.GetPosition(image).X;
                (popup1.Child as Image).Source = new BitmapImage((image.Source as BitmapImage).UriSource);
                mouseVerticalPosition = e.GetPosition(null).Y;
                mouseHorizontalPosition = e.GetPosition(null).X;
                isMouseCaptured = true;
                popup1.IsOpen = true; ;
                img.CaptureMouse();
            }
        }

        private void StackPanel_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!isMouseCaptured)
                return;

            
            double deltaV = e.GetPosition(this).Y - mouseVerticalPosition;
            double deltaH = e.GetPosition(this).X - mouseHorizontalPosition;
            double newTop = deltaV + (double)popup1.VerticalOffset;
            double newLeft = deltaH + (double)popup1.HorizontalOffset;           
            popup1.VerticalOffset = newTop;
            popup1.HorizontalOffset = newLeft;           
            mouseVerticalPosition = e.GetPosition(this).Y;
            mouseHorizontalPosition = e.GetPosition(this).X;
        }

        private void StackPanel_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (isMouseCaptured)
            {
                double xcord = e.GetPosition(spPlayer).X;
                double ycord = e.GetPosition(spPlayer).Y;
                if (xcord <= spPlayer.ActualWidth && ycord <= spPlayer.ActualHeight)
                {
                    mouseVerticalPosition = -1;
                    mouseHorizontalPosition = -1;                  
                    string vidName = DataAcces.GetVideoName(((BitmapImage)img.Source).UriSource.OriginalString);
                    if (vidName.Length > 0)
                    {
                        myPlayer.Source = new Uri("http://localhost/SilverLightMediaPlayer/MediaPlayerLive/ClientBin/" + vidName, UriKind.Absolute);
                        myPlayer.Play();                        
                    }
                    sbarHide.Begin();   
                }
                (popup1.Child as Image).Source = null;
                popup1.IsOpen = false;
                isMouseCaptured = false;
                img.ReleaseMouseCapture();
                img = null;
            }
        }

        private void stackMenu_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.OriginalSource is Image && ((StackPanel)sender).Opacity > 0)
            {
                Image imgSender = (Image)e.OriginalSource;
                
                string vidName = DataAcces.GetVideoName(((BitmapImage)imgSender.Source).UriSource.OriginalString);
                if (vidName.Length > 0)
                {
                    myPlayer.Source = new Uri("http://localhost/SilverLightMediaPlayer/MediaPlayerLive/ClientBin/" + vidName, UriKind.Absolute);
                    myPlayer.Play();
                }
            }
        }

        private void ScrollViewer_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (sbar.Width == 0)
            {
                sbar.Visibility = Visibility.Visible;
                sbarShow.Begin();
                txtCPanels.Text = "<" + Environment.NewLine + "<";
            }
        }

        private void sbarHide_Completed(object sender, EventArgs e)
        {
            sbar.Visibility = Visibility.Collapsed;
            txtCPanels.Text = ">" + Environment.NewLine + ">";
        }

        private void Rectangle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (sbar.Width > 0)
            {
                sbarHide.Begin();
            }
        }     
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

sunit_82
Software Developer (Senior) Geometric
India India
B.E. in Information Technology
MCTS(.NET 2.0 )

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 6 Apr 2009
Article Copyright 2009 by sunit_82
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid