Click here to Skip to main content
15,896,063 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 370.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.Controls;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Data;
using System.Diagnostics;

namespace Sonic
{

    

    /// <summary>
    /// ViewModel for MainWindow View
    /// </summary>
    public class MainWindowViewModel : ViewModelBase
    {



        #region Data

        private List<String> genres = null;
        private List<String> artistLetters = null;
        private MediaViewViewModel mediaViewVM = new MediaViewViewModel();

        private Boolean isBusy = false;

        #endregion

        #region Ctor
        public MainWindowViewModel()
        {

        }
        #endregion

        #region Public Methods

        public void Initialise()
        {
            IsBusy = true;

            //Setup Genrees
            if (genres == null)
            {
                genres = new List<string>();

                using (StreamReader sr = new StreamReader("MP3TagGenres.txt"))
                {
                    String[] allGenres = sr.ReadToEnd().Split(
                        new String[] { @"\r", "\n" },
                        StringSplitOptions.RemoveEmptyEntries);

                    foreach (String genreRead in allGenres)
                    {
                        genres.Add(genreRead.Trim());
                    }
                }
                Genres = genres;
            }
           
            //Setup Artist Letters
            if (artistLetters == null)
            {
                artistLetters = new List<string>();
                for (int i = 65; i < 91; i++)
                    artistLetters.Add(((char)i).ToString());

                ArtistLetters = artistLetters;
            }

            if (Globals.ReReadAllFiles)
            {

                try
                {
                    //use a Threadpool thread to run the query
                    ThreadPool.QueueUserWorkItem(x =>
                    {
#if DEBUG
                        Stopwatch stopWatch = new Stopwatch();
                        stopWatch.Start();
                        Console.WriteLine("=========== START OF DB SAVING ===========");
#endif
                        IsBusy = true;
                        mediaViewVM.ReadInitialFolders();
                        XMLAndSQLQueryOperations.CreateDB_MP3Files();
#if DEBUG
                        stopWatch.Stop();
                        TimeSpan ts = stopWatch.Elapsed;
                        String elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                            ts.Hours, ts.Minutes, ts.Seconds,
                            ts.Milliseconds / 10);

                        Console.WriteLine(String.Format(" It took {0} to run the query",
                            elapsedTime));
                        Console.WriteLine("=========== END OF DB SAVING ===========");
#endif
                        IsBusy = false;
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Ooops, its busted " + ex.Message);
                }
                finally
                {
                    IsBusy = false;
                }
            }
            else
            {
                IsBusy = false;
            }
        }


        #endregion

        #region Public Properties

        public Boolean IsBusy
        {
            get { return isBusy; }
            set
            {
                isBusy = value;
                NotifyPropertyChanged("IsBusy");
                NotifyPropertyChanged("IsNotBusy");
            }
        }

        public Boolean IsNotBusy
        {
            get { return !isBusy; }
        }


        public List<String> Genres
        {
            get { return genres; }
            set
            {
                genres = value;
                NotifyPropertyChanged("Genres");
            }
        }

 
        public List<String> ArtistLetters
        {
            get { return artistLetters; }
            set
            {
                artistLetters = value;
                NotifyPropertyChanged("ArtistLetters");
            }
        }

        public MediaViewViewModel MediaViewVM
        {
            get { return mediaViewVM; }
            set
            {

                mediaViewVM = value;
                NotifyPropertyChanged("MediaViewVM");
            }
        }
        #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