Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / C#

LyricsFetcher - The Easiest Way to Find Lyrics for your Songs

Rate me:
Please Sign up or sign in to vote.
4.93/5 (82 votes)
29 Oct 2009GPL325 min read 201.2K   2.4K   184  
An article describing the development of a non-trivial C#/.NET application to fetch lyrics for songs.
/*
 * The file contains all the Windows Media Player specific code.
 *
 * Author: Phillip Piper
 * Date: 7/01/2008 8:40 PM
 *
 * CHANGE LOG:
 * 2008-01-07 JPP  Initial Version
 */

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using WMPLib;

namespace LyricsFetcher
{
    public sealed class Wmp
    {
		#region Instance access and constructors

        /// <summary>
        /// Return the singleton instance of this class
        /// </summary>
		public static Wmp Instance {
			get { return instance; }
		}
        private static Wmp instance = new Wmp();

        /// <summary>
        /// Is Windows Media Player actually installed on this machine?
        /// </summary>
        /// <remarks>If this returns false, no other methods should be called.</remarks>
        /// <returns></returns>
        public static bool HasWmp()
        {
            return (Wmp.Instance.Player != null);
        }

        /// <summary>
        /// You cannot create instances of Wmp. You access a singleton through
        /// "Wmp.Instance".
        /// </summary>
		private Wmp()
		{
		}

		#endregion

        public const string WMTitle = "Title";
        public const string WMAuthor = "Author";
        public const string WMDescription = "Description";
        public const string WMRating = "Rating";
        public const string WMCopyright = "Copyright";
        public const string WMAlbumTitle = "WM/AlbumTitle";
        public const string WMTrack = "WM/Track";
        public const string WMGenre = "WM/Genre";
        public const string WMYear = "WM/Year";
        public const string WMGenreID = "WM/GenreID";
        public const string WMComposer = "WM/Composer";
        public const string WMLyrics = "WM/Lyrics";
        public const string WMTrackNumber = "WM/TrackNumber";
        public const string WMAlbumArtist = "WM/AlbumArtist";

		#region Public attributes

		/// <summary>
		/// Return the window media player
		/// </summary>
		public WindowsMediaPlayer Player
		{
			get {
                // TODO: Put this in a critical section
                if (this.wmpPlayer == null) {
					try {
                        this.wmpPlayer = new WindowsMediaPlayer();
					} catch (Exception) {
						// do nothing
					}
				}
                return this.wmpPlayer;
			}
		}
        private WindowsMediaPlayer wmpPlayer;

        /// <summary>
        /// Returns a track collection of all the tracks available in the WMP library
        /// </summary>
        /// <returns>IWMPPlaylist</returns>
        public IWMPPlaylist AllTracks {
            get {
                return this.Player.mediaCollection.getByAttribute("MediaType", "audio");
            }
        }


        #endregion

        #region Event handlers

        void wmpPlayer_MediaError(object pMediaObject) {
            System.Diagnostics.Debug.WriteLine("Cannot play media file.");
        }

        void wmpPlayer_PlayStateChange(int newState) {
            System.Diagnostics.Debug.WriteLine(String.Format("PlayStateChanged: {0}", (WMPPlayState)newState));
        }

        #endregion

        #region Private variables


        #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 GNU General Public License (GPLv3)


Written By
Team Leader
Australia Australia
Phillip has been playing with computers since the Apple II was the hottest home computer available. He learned the fine art of C programming and Guru meditation on the Amiga.

C# and Python are his languages of choice. Smalltalk is his mentor for simplicity and beauty. C++ is to programming what drills are to visits to the dentist.

He worked for longer than he cares to remember as Lead Programmer and System Architect of the Objective document management system. (www.objective.com)

He has lived for 10 years in northern Mozambique, teaching in villages.

He has developed high volume trading software, low volume FX trading software, and is currently working for Atlassian on HipChat.

Comments and Discussions