Click here to Skip to main content
15,392,226 members
Articles / Desktop Programming / WPF
Posted 7 Mar 2010


18 bookmarked

An Audiobook Player Using an Embedded Microsoft Mediaplayer

Rate me:
Please Sign up or sign in to vote.
3.64/5 (7 votes)
7 Mar 2010CPOL4 min read
An audiobook player using an embedded Microsoft mediaplayer


This project is born out of pure frustration with Microsoft Mediaplayer and Itunes. The thing I needed was a simple player for my audio books that remembers the last position (file index and position).

Some of my audio books are a composed of several MP3 files (not all containing full meta information). So it would be nice to get a cover image if none is provided and to guess the author and title. The application is a hobby project, not a commercial grade application. So don't expect any unit tests or any other guaranties, it works on my machine (A Windows 7 64-bit laptop). It’s placed on CodeProject to give something back to everybody posting nice articles on the site. May you find the application interesting or the code useful. It contains the full source code and a setup project for the executable.

Application Requirements

Remember Position

There is only 1 playlist (the current). It will be written to the application data directory when the application closes and read at application startup. The playlist (class AudiobookList.cs) contains the names of the files, the cover image and the current playing position.

Easy to Create New Playlist

Support for drag-and-drop. All files dropped on the application will form a new playlist and start playing. The files can also be added to the commandline startup parameters.

Read Meta-Info

The taglib is used to read the meta information from a media file.

Guess meta-info Based on Filename

Use the Amazon webstore to check artist and title and get the cover image. You will need to supply your own account key. This key can be obtained without cost from Amazon (

Implementation Details


The application is built using WPF and is a border-less application. The border can be hidden by adding the following to the declaration of the window:

<window ResizeMode="NoResize" WindowStyle="None" AllowsTransparency="True"

Adding a leftmouse down eventhandler adds support for moving the window around on the screen when the left mouse button is held, while moving the mouse.

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    if (sender == this)

The main window is still called Window1 because I am a bit lazy. ;-) The code is straight-forward without any tricks, other than some opacity changes on bitmaps.


The playlist is stored as an instance of AudiobookList. The class itself contains all the code needed to create, load and store the list. Based on a list of files or a directory, a new instance can be created. The directories will be scanned recursively and each file will be added if it’s an audio file. This is done by getting the mime-type from the registry based on the file extension. If it starts with “audio”, we assume it’s an audio file.
The filenames are sorted based on the numbers in the file and the containing directory. Every number is added, multiplying the total with 1000 for each number found.

public static FileType DetermineFileType( string filename )
    string mime = MimeType(filename);
    if (mime.StartsWith("image"))
        return FileType.Image;
    else if (mime.StartsWith("audio"))
        return FileType.Audio;
    return FileType.Other;

private static string MimeType(string filename)
    string mime = "application/octetstream";
    string ext = System.IO.Path.GetExtension(filename).ToLower();
    Microsoft.Win32.RegistryKey rk = 
    if (rk != null && rk.GetValue("Content Type") != null)
        mime = rk.GetValue("Content Type").ToString();
    return mime;


The class DetectMediaInfo will retrieve the meta info from a file. It will try the MP3 tags first using the open-source TagLib library. If no information is provided in the meta-info, we try to extract the author and title from the filename. We use the Amazon web store to check them. The parameters for Amazon are added to a URL and hashed using the access and secret key (not provided).The access and secret key can be entered using the config page and will be stored in the application config file. The parameters for the Amazon WS are hashed and added to the URL. The response is plain XML. The code itself was provided by Amazon. The only thing I added was the processing of the resulting XML with LINQ.

public void FindBookByTitle(string searchTitle, string searchAuthor, 
	out string bookTitle, out string bookArtist, out string imageUrl) 
    bookTitle = string.Empty; 
    bookArtist = string.Empty; 
    imageUrl = string.Empty; 
    string url = BuildRequestSearchBookTitle(searchTitle, searchAuthor); 
    string xml = CallAmazonWS(url); 
    ProcessXmlBookResult(xml, ref bookTitle, ref bookArtist, ref imageUrl); 

private string BuildRequestSearchBookTitle(string searchTitle, 
					string searchAuthor) 
    IDictionary<string, /> parameters = new Dictionary<string, />(); 
    parameters["Service"] = "AWSECommerceService"; 
    parameters["Version"] = "2009-03-31"; 
    parameters["Operation"] = "ItemSearch"; 
    parameters["SearchIndex"] = "Books"; 
    parameters["Title"] = searchTitle; 
    if (string.IsNullOrEmpty(searchAuthor) == false) 
        parameters["AuthorName"] = searchAuthor; 
    parameters["ResponseGroup"] = "ItemAttributes,Images"; 
    string requestUrl = Sign(parameters); 
    return requestUrl; 

private static string CallAmazonWS(string url) 
    WebRequest request = HttpWebRequest.Create(url); 
    WebResponse response = request.GetResponse(); 
    StreamReader reader = new StreamReader( response.GetResponseStream( ) ); 
    return reader.ReadToEnd( ); 

private static void ProcessXmlBookResult(string xml, ref string bookTitle, 
			ref string bookArtist, ref string imageUrl) 
    XNamespace ns = ""; 
    XElement root = XElement.Parse( xml ); 
    bool isValid = RequestIsValid(root, ns); 
    int resultCount = GetResultCount(root, ns); 
    if ( isValid && (resultCount > 0)) 
        XElement item = GetFirstItem(root, ns); 
        GetAuthorAndTitle(item, ns, ref bookTitle, ref bookArtist); 
        imageUrl = GetItemImage( item, ns ); 

The Player

The player is a COM interface of Microsoft Mediaplayer, hence the WMPLIB interop library. The MyPlayer class is an abstraction of the COM instance. We cast the interface to a WindowsMediaPlayerClass instance. This allows us to receive PlayStateChange event and set the Autostart property to false. There is no need to do anything fancy. The UI has a background timer that is run every second. It checks if the player is playing and then prints the current duration as a string. A duration string is provided if the Mediaplayer is not yet playing, otherwise we get an empty string.

Ideas to Improve the Application

  • The application assumes there is only one author and title for all the files. If you add different titles to the list, only the first will be shown.
  • The XML result from Amazon only processes the first item. Maybe the other items may also contain a valid result.
  • Add some more checks and add some robustness. Currently only the real bare minimum of the exceptions are trapped with a try/catch.
  • Add support for formats other than MP3. It may work, but none is tested.
  • Use the cloud as a storage for audio files and AudiobookList. So play may continue on every computer used.

Have fun!!


  • 7th March, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

André van heerwaarde
Software Developer Eclectic (
Netherlands Netherlands
I am software developer working with microsoft technologies for the last 20 years. Seen almost all versions of DOS and windows and programming in Assembly, C / C++ / C# and VB.NET. Currently focussing completely on everything .NET.

Comments and Discussions

Questiondropping files on Win 10 version not working. Pin
Mark Wallace Dec202128-Dec-21 7:56
MemberMark Wallace Dec202128-Dec-21 7:56 
QuestionWorks well on Windows 10 Pin
Member 1509577610-Mar-21 12:52
MemberMember 1509577610-Mar-21 12:52 
BugDoesn't work - asking for Amazon AWS key! Pin
Member 1480692219-Apr-20 10:59
MemberMember 1480692219-Apr-20 10:59 
GeneralRe: Doesn't work - asking for Amazon AWS key! Pin
Mark Wallace Dec202128-Dec-21 7:58
MemberMark Wallace Dec202128-Dec-21 7:58 
GeneralRe: Doesn't work - asking for Amazon AWS key! Pin
Member 1551800930-Jan-22 13:23
MemberMember 1551800930-Jan-22 13:23 
QuestionI'm using Windows 10 but it's asking for .NET Framework 3.5 Pin
Member 1478728330-Mar-20 5:05
MemberMember 1478728330-Mar-20 5:05 
QuestionAudiobook Player Failed to Install Pin
Member 140826929-Dec-18 0:09
MemberMember 140826929-Dec-18 0:09 
QuestionMultiple audio books Pin
Roman R.8-Mar-13 19:03
MemberRoman R.8-Mar-13 19:03 
GeneralWorking on Win8 x64 Pin
DerekWright7131-Jan-13 4:04
MemberDerekWright7131-Jan-13 4:04 
QuestionNice Start Pin
MarkSchulz13-Dec-12 4:25
MemberMarkSchulz13-Dec-12 4:25 
AnswerRe: Nice Start Pin
André van heerwaarde13-Dec-12 14:28
MemberAndré van heerwaarde13-Dec-12 14:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.