avatar

professional geek ramblings
est. 2003
About

CodeSnip: Getting Currently Playing info from Windows Media Player using the blogging power toy.

I don’t know whom this would benefit but someone building a blog posting tool for the desktop, but anyway here’s the code that I converted from the jscript example that comes with the Windows Media Player blogging powertoy :

using System;
using Microsoft.Win32;

public class MediaPlayerInfo
	{

		public static string GetCurrentlyPlayingMedia(){
			RegistryKey regKey = Registry.CurrentUser;
			regKey = regKey.OpenSubKey("Software\\Microsoft\\MediaPlayer\\CurrentMetadata");

			string displayString = "<div class='media'>[ Currently Listening to: ";

			bool hasMetadata = false;

			string trackInfo = "";

			try{
				trackInfo = regKey.GetValue("Title").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += trackInfo + " ";
				}
			}catch{
				try{
					trackInfo = "";
					trackInfo = regKey.GetValue("Name").ToString();

					if(trackInfo.Length != 0){
						hasMetadata = true;

						displayString += trackInfo + " ";
					}
				}catch{}
			}

			try{
				trackInfo = "";
				trackInfo = regKey.GetValue("DurationString").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += "(" + trackInfo + ")";
				}
			}catch{}

			try{
				trackInfo = "";
				trackInfo = regKey.GetValue("Author").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += " by " + trackInfo;
				}
			}catch {}

			try{
				trackInfo = "";
				trackInfo = regKey.GetValue("Album").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += " on the album " + trackInfo;
				}
			}catch{}


			if(!hasMetadata){
				displayString += "Nothing.";
			}

			displayString += " ]</div>";

			return displayString;
		}
	}

It could probably stand to be a bit more flexible - ie user-defined by a template like w.bloggar, but for the basics it works great! Oh yeah, and let’s not forget the output:

[ Currently Listening to: Rage Against The Machine - Pocket Full of Shells (03:52) ]