Click here to Skip to main content
15,895,746 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.8K   2.4K   184  
An article describing the development of a non-trivial C#/.NET application to fetch lyrics for songs.
/*
 * This file holds the class implementations necessary to interact with Window Media Library.
 *
 * Author: Phillip Piper
 * Date: 2009-03-14 8:28 AM
 *
 * CHANGE LOG:
 * 2009-03-19 JPP  Added FullPath property
 * 2009-03-14 JPP  - Changed to use MetaDataEditor to update lyrics
 *                 - Separated from WmpLibrary.cs
 */

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

using WMPLib;

namespace LyricsFetcher
{
    /// Instances of these songs know how to load and store
    /// themselves from WindowMediaPlayer store.
    /// </summary>
    public class WmpSong : Song
    {
        /// <summary>
        /// Create a new song from the given media object
        /// </summary>
        /// <param name="iWmpMedia">The media object that represents the song</param>
        public WmpSong(IWMPMedia iWmpMedia) {
            this.Media = iWmpMedia;
            this.Title = iWmpMedia.getItemInfo(Wmp.WMTitle);
            this.Artist = iWmpMedia.getItemInfo(Wmp.WMAuthor);
            this.Album = iWmpMedia.getItemInfo(Wmp.WMAlbumTitle);
            this.Genre = iWmpMedia.getItemInfo(Wmp.WMGenre);
        }

        /// <summary>
        /// Get or set the media object that represents the song
        /// </summary>
        public IWMPMedia Media { get; set; }

        /// <summary>
        /// Return the full path to the underlying media file
        /// </summary>
        public override string FullPath {
            get {
                return this.Media.sourceURL;
            }
        }

        /// <summary>
        /// Read the lyrics of this song from the WMP library.
        /// </summary>
        public override void GetLyrics() {
            this.Lyrics = this.Media.getItemInfo(Wmp.WMLyrics);
        }

        /// <summary>
        /// Write the current state of this song back into WMP
        /// </summary>
        public override void Commit() {
            if (this.Media == null)
                return;

            if (this.Media.isReadOnlyItem(Wmp.WMTitle) == false)
                this.Media.setItemInfo(Wmp.WMTitle, this.Title);
            if (this.Media.isReadOnlyItem(Wmp.WMAuthor) == false)
                this.Media.setItemInfo(Wmp.WMAuthor, this.Artist);
            if (this.Media.isReadOnlyItem(Wmp.WMAlbumTitle) == false)
                this.Media.setItemInfo(Wmp.WMAlbumTitle, this.Album);
            if (this.Media.isReadOnlyItem(Wmp.WMGenre) == false)
                this.Media.setItemInfo(Wmp.WMGenre, this.Genre);

            if (this.Media.isReadOnlyItem(Wmp.WMLyrics) == false &&
                this.Media.getItemInfo(Wmp.WMLyrics) != this.Lyrics) {
                /*
                 * Life is never easy. We should be able to simply say:
                 * this.Media.setItemInfo(Wmp.WMLyrics, this.Lyrics);
                 * but that doesn't work. On WMP 9 and 10, it doesn't update
                 * the lyrics at all. On WMP 11, it updates them, but creates
                 * hundreds of entries -- one for each language.
                 * So we have to do something more complicated
                 */
                using (MetaDataEditor editor = new MetaDataEditor(this.Media.sourceURL)) {
                    editor.SetFieldValue(Wmp.WMLyrics, this.Lyrics);
                }
            }
        }
    }
}

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