Thursday, June 26, 2008

Windows Media Player (and Center) library interface for .NET

I have had a few problems learning how to do stuff with Windows Media Center.
The API is ok, but its by no means complete and as such really restricts the amount you can do with it.
Its a start, and somewhere somehow the tools are there to let you do what you want with Windows, its just a matter of how hard you want to work.

I wanted a way to programmatically access the music library for Windows Media Center for an application I am developing. After crawling through forums and API's I soon worked out it just wasnt going to happen... but something obvious occured to me: The Media Center library is just freaking Media Player! Media Center is just a glorified (and pretty sweet) front end for WMP.

So I spent some time medling with getting access to the WMP SDK from Visual Studio Express 2008 in C#. It was REALLY easy, but here is a crash course.

Grab your self some Windows Media Player SDK if you want, but I am really not sure that you need it anymore.

Make a new C# (or what ever language you like) project. Right click on the References and Add Reference. We want a COM object, and its quite simple Windows Media Player which links right to the wmp.dll.
All you need in code then is a "using WMPLib" or what ever the include work is for your language.

Let me point out here that I am UBER impressed with how easy it is to include COM objects in to managed code. Really very cool.

Forgive me, at this moment I have no idea how to put code into the blog with styles so its going to be a bit crappy to look at.

You are going to need a few objects to do work with. First is a WindowsMediaPlayer object.

WindowsMediaPlayer player = new WindowsMediaPlayer();


So you want access to all your music? Ok, easy.


IWMPPlaylist playList = Player.mediaCollection.getByAttribute("MediaType", "audio");

Simple! Just bounce through that big list of music and do what you want. All the properties and attributes of the files are accessed through the IWMPMedia class by invoking a getItemInfo.

IWMPMedia = playList.get_item(i); // i am obviously doing this in a loop arent I :-)
string artist = currentMedia.getItemInfo("DisplayArtist");


I cant remember how I worked out what the item tags were called, but I think I just looked at the inside of the IWMPMedia through the debugger.
"Title" is another useful attribute you might want to query.

What if I wanted to play this song in Media Center you ask? Easy. I won't go through the in's and out's of the Media Center API, but just get the location of the song via "currentMedia.sourceURL" and pass that to WMC's PlayMedia method.

I hope I have helped someone out with this info

2 comments:

Anonymous said...

wait.. you dont talk about spanish folk dancing?? i got really excited for a minute, as im looking to start learning how to do that.

Anonymous said...

ah i see.. i was searching for starcraft blogs originally and saw spanish folk dancing aswell xD. is there any other way to msg someone other than leaving comments? i started my blog literally last weekend, lol