Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / WPF

Sonic: A WPF (hybrid smart client) searchable media library

Rate me:
Please Sign up or sign in to vote.
4.87/5 (228 votes)
21 Feb 2009CPOL20 min read 366.2K   4.6K   309  
A queryable working MP3 player, using some cool LINQ stuff.
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Sonic
{

    public delegate void MP3FileClickedEventHandler(object sender, MP3FileClickedEventArgs e);


    /// <summary>
    /// Interaction logic for MP3FileView.xaml
    /// </summary>
    public partial class MP3FileView : UserControl
    {
        public MP3FileView()
        {
            InitializeComponent();

            this.DataContextChanged += MP3FileView_DataContextChanged;

        }

        #region Events
        /// <summary>
        /// Raised when Album item clicked
        /// </summary>
        public static readonly RoutedEvent MP3FileClickedEvent =
                EventManager.RegisterRoutedEvent(
                "MP3FileClicked", RoutingStrategy.Bubble,
                typeof(MP3FileClickedEventHandler),
                typeof(MP3FileView));




        public event MP3FileClickedEventHandler MP3FileClicked
        {
            add { AddHandler(MP3FileClickedEvent, value); }
            remove { RemoveHandler(MP3FileClickedEvent, value); }
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Hook up the associated MP3FileViewModel
        /// AnimationStartTimerExpiredEvent event
        /// </summary>
        private void MP3FileView_DataContextChanged(object sender, 
            DependencyPropertyChangedEventArgs e)
        {
            MP3FileViewModel viewModel = e.NewValue as MP3FileViewModel;
            if (viewModel != null)
            {
                viewModel.StartDelayedAnimationTimer();
                viewModel.AnimationStartTimerExpiredEvent += 
                    ViewModel_AnimationStartTimerExpiredEvent;
            }
        }

        /// <summary>
        /// Start the animation Storyboard after the associated AlbumOfMP3ViewModel
        /// timer expires and raises its AnimationStartTimerExpiredEvent event
        /// </summary>
        private void ViewModel_AnimationStartTimerExpiredEvent(object sender, EventArgs e)
        {
            //As the call that populated this control was on a different thread, 
            //we need to do some threading trickery
            this.Dispatcher.InvokeIfRequired(() =>
            {
                Storyboard sb = this.TryFindResource("OnLoaded1") as Storyboard;
                if (sb != null)
                {
                    sb.Begin(this.spMP3s);
                }
            }, DispatcherPriority.Normal);
        }

        /// <summary>
        /// When an item is clicked raise the event so that the MediaView can
        /// play the selected file
        /// </summary>
        private void SpMP3s_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //raise our custom AlbumClickedEvent event
            MP3FileClickedEventArgs args = new
                MP3FileClickedEventArgs(MP3FileClickedEvent,
                    (this.DataContext as MP3FileViewModel).FileName);
            RaiseEvent(args);
        }
        #endregion


    }
}

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)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions