client/src/Iri.Modernisation.Controls/View/VideoViewer/VideoViewer.xaml.cs
author totetm <>
Thu, 04 Feb 2010 16:38:04 +0100
changeset 38 bd33267300aa
parent 36 b6df6fce6e5d
permissions -rw-r--r--
- FranceCulture Project - Ldt and Iri Parse Method - Bug MenuableUserControl

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Iri.Modernisation.BaseMVVM.Commands;
using Iri.Modernisation.Controls.ViewModel;
namespace Iri.Modernisation.Controls.View
{
	public partial class VideoViewer : UserControl
	{


     
        public System.Windows.Threading.DispatcherTimer VideoPositionTimer = new System.Windows.Threading.DispatcherTimer();
       
        public String MediaSource
        {
            get
            { 
               
                return (String)GetValue(SourceProperty); 
            }
            set 
            { 
                SetValue(SourceProperty, value);
                if(DataContext != null)
                ((VideoViewerVM)DataContext).Source = value;
            }
        }

        // Using a DependencyProperty as the backing store for Source.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("MediaSource", typeof(String), typeof(VideoViewer), new PropertyMetadata(""));

        private bool _isPlayed  = false;
		public VideoViewer()
		{
			// Required to initialize variables
			InitializeComponent();   /// <summary>
            VideoScreen.MediaEnded += new RoutedEventHandler(VideoScreen_MediaEnded);
            VideoScreen.MediaOpened += new RoutedEventHandler(VideoScreen_MediaOpened);
            VideoPositionTimer.Interval = new System.TimeSpan(0, 0, 0, 0, 500); 
            VideoPositionTimer.Tick += new EventHandler(VideoPositionTimer_Tick);
            Commands.GoToTime.Executed += new EventHandler<SLExtensions.Input.ExecutedEventArgs>(GoToTime_Executed);
            Commands.VideoViewer.Pause.Executed += new EventHandler<SLExtensions.Input.ExecutedEventArgs>(Pause_Executed);
            Commands.VideoViewer.Play.Executed +=new EventHandler<SLExtensions.Input.ExecutedEventArgs>(Play_Executed);

      
            
        }

        void VideoScreen_MediaOpened(object sender, RoutedEventArgs e)
        {
            /*
             * In Order to begin 
             */
            VideoScreen.Position = ((VideoViewerVM)DataContext).BeginIn;
            //VideoScreen.Play();
            //VideoPositionTimer.Start();
            
        }

        void VideoScreen_MediaEnded(object sender, RoutedEventArgs e)
        {
            VideoPositionTimer.Stop();
        }

        private void GoToTime_Executed(object sender, SLExtensions.Input.ExecutedEventArgs e)
        {
            if (e.Source == DataContext && e.Source != null)
            {
                //if(!VideoPositionTimer.IsEnabled)
                //VideoPositionTimer.Start();
               
               VideoScreen.Position = ((TimeSpan)e.Parameter);
               
              
            }
        }

        void VideoPositionTimer_Tick(object sender, EventArgs e)
        {
            if (_isPlayed) 
            {
                if(Tick!=null)
                {
                    Tick(this, new VideoViewerEventArgs(VideoScreen.Position));
                }
                ((VideoViewerVM)DataContext).LaunchTick(VideoScreen.Position);
            }
           //Commands.VideoViewer.SendPosition.Execute(VideoScreen.Position);
        }
        public void ChangePosition(TimeSpan TS)
        {
            VideoScreen.Position = TS;
            
        }
        void Pause_Executed(object sender, SLExtensions.Input.ExecutedEventArgs e)
        {
            if(e.Source == DataContext || e.Parameter == DataContext)
            {
                VideoScreen.Pause();
                _isPlayed = false;
                VideoPositionTimer.Stop();
                if (DataContext != null)
                {
                    ((VideoViewerVM)DataContext).AutoPlay = false;
                    ((VideoViewerVM)DataContext).IsPLayed = false;
                }
            }
        }

        void Play_Executed(object sender, SLExtensions.Input.ExecutedEventArgs e)
        {
            if ((e.Parameter == DataContext || e.Parameter == null) && VideoScreen.Source != null)
            {
                _isPlayed = true;
                VideoScreen.Play();
                VideoPositionTimer.Start();
                {
                    ((VideoViewerVM)DataContext).AutoPlay = true;
                    ((VideoViewerVM)DataContext).IsPLayed = true;
                }
            }
        }

		private void VideoScreen_MediaFailed(object sender, System.Windows.ExceptionRoutedEventArgs e)
		{
			MessageBox.Show(e.ErrorException.ToString());
		}

		

		private void VideoScreen_MarkerReached(object sender, System.Windows.Media.TimelineMarkerRoutedEventArgs e)
		{
            MessageBox.Show("toto");
		}
        public event EventHandler<VideoViewerEventArgs> Tick;
	}
    public class VideoViewerEventArgs: EventArgs
    {

        public TimeSpan Position { get; set; }
        public VideoViewerEventArgs(TimeSpan pos)
        {
            Position = pos;
        }
    }
}