• behance
  • gitHub
  • google
  • linkedin
  • twitter
Using MediaElement

Using MediaElement

I’ve just put together a really dirty and VERY simple app that uses the MediaElement class in .NET 3.5 as an example for someone on CodeProject so I thought I’d post it here as it’s lying around on my web server.

Here is the class:

    public partial class Window1 : Window
    {
        ObservableCollection _filestoplay = new ObservableCollection();

        public Window1()
        {
            this.InitializeComponent();
            this.lstFilesToPlay.SelectionChanged += new SelectionChangedEventHandler(lstFilesToPlay_SelectionChanged);

            DirectoryInfo dirinfo = new DirectoryInfo(@"G:general storagefilms");
            FileInfo[] _files = dirinfo.GetFiles("*.avi");
            foreach (FileInfo file in _files)
            {
                _filestplay.Add(file.FullName.ToString());
            }
            lstFilesToPlay.ItemsSource = _filestoplay;
        }

        void lstFilesToPlay_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string _filetoplay = lstFilesToPlay.SelectedItem.ToString();
            MediaElement mediaElementPlayer = new MediaElement();
            mediaElementPlayer.LoadedBehavior = MediaState.Manual;
            mediaElementPlayer.Width = 200;
            mediaElementPlayer.Height = 200;
            mediaElementPlayer.Source = new Uri(_filetoplay, UriKind.RelativeOrAbsolute);
            mediaElementPlayer.Play();
            LayoutRoot.Children.Add(mediaElementPlayer);
        }
    }

You can download the solution file here.

Leave a reply

*