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

(Hybrid Smart Client) RSS Media Player

, 9 Mar 2009
A WPF application for viewing RSS video feeds. Built using the MVVM pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;

namespace RSSVideoPlayer.MediaElementControl
{
    /// <summary>
    /// Interaction logic for MediaControl.xaml
    /// </summary>
    public partial class MyMediaControl : UserControl, INotifyPropertyChanged
    {
        //Declare / register the dependancy Prop
      
        private DispatcherTimer DTimer;
        private static Uri _MediaURI;
        private bool _userUpdatingTime;
        public MyMediaControl()
        {
            InitializeComponent();
            DTimer = new DispatcherTimer(DispatcherPriority.Background, mMediaElement.Dispatcher);
            DTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
            DTimer.Tick += new EventHandler(DTimer_Tick);
            DTimer.Start();
            this.DataContext = this;
            mMediaElement.Loaded += new RoutedEventHandler(mMediaElement_Loaded);
         
        }

        void mMediaElement_Loaded(object sender, RoutedEventArgs e)
        {
            mMediaElement.LoadedBehavior = MediaState.Manual;
            mMediaElement.UnloadedBehavior = MediaState.Manual;

        }

        #region "Properties"
        public static readonly DependencyProperty MediaSourceProperty =
          DependencyProperty.Register("MyMediaSource", typeof(string), typeof(MyMediaControl), new PropertyMetadata(LastNameChangedCallback));
        public string MyMediaSource
        {
            get
            {
                return (string)GetValue(MediaSourceProperty);
            }
            set
            {
                SetValue(MediaSourceProperty, value);
            }
        }


        private static void LastNameChangedCallback(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
                if (e.NewValue.ToString().Length > 0)
                    _MediaURI = new Uri((string)e.NewValue);
        }

        #endregion

        private void mMediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            sldTimeLine.Maximum = mMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        void DTimer_Tick(object sender, EventArgs e)
        {
            //It is important to make sure the media element has loaded.
            if (mMediaElement != null && mMediaElement.IsLoaded)
            {
                if (_MediaURI != mMediaElement.Source)
                {

                    mMediaElement.Source = new Uri(_MediaURI.ToString()) ;
                 
                    mMediaElement.Play();
         
                }
                if (!_userUpdatingTime)
                    sldTimeLine.Value = mMediaElement.Position.TotalMilliseconds;
                txtBuffer.Text = (mMediaElement.BufferingProgress * 100).ToString();


                TimeSpan ts = mMediaElement.Position;
                StringBuilder sb = new StringBuilder();
                sb.Append(ts.Hours + ":");
                sb.Append(ts.Minutes + ":");
                sb.Append(ts.Seconds);
                txtPosition.Text = sb.ToString();

            }
        }

        public void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            mMediaElement.Play();
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            mMediaElement.Pause();
        }

        private void mMediaElement_Unloaded(object sender, RoutedEventArgs e)
        {
            mMediaElement.Close();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            mMediaElement.Stop();
        }

        private void sldTimeLine_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _userUpdatingTime = true;
        }

        private void sldTimeLine_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if(mMediaElement !=null && mMediaElement.NaturalDuration.HasTimeSpan && this._userUpdatingTime)
            mMediaElement.Position = new TimeSpan(0, 0, 0, 0, (int)sldTimeLine.Value);
            _userUpdatingTime = false;
        }

   
    }
}

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 The Code Project Open License (CPOL)

Share

About the Author

Paul_Wade
Software Developer (Senior) Anytime Fitness
United States United States
No Biography provided
Follow on   Twitter

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 9 Mar 2009
Article Copyright 2009 by Paul_Wade
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid