Help with a simple Visual C# coding problem?
December 1, 2009 8:07 AM   Subscribe

Oh God. Where to start? This is, at its heart, a programming question about C#. It's probably not a very hard programming question, either.

Although it is against my nature, I will attempt to be brief. I recently built a Windows Media Center PC and hooked it up to my TV and I'm really loving the Windows 7 media center experience... except that all the music I've accumulated for the last 10 years or so is in iTunes. So, while I can watch movies and TV shows and browse my pictures and be a multimedia guru, if I want to listen to music I have to get up off the couch, close media center, launch iTunes and then pump up the volume.

On some level this really irritates me.

I explored exhaustively solutions that exist out in the wild. MCE Tunes looks like a great application that will basically allow you to access iTunes from inside WMC, but it doesn't work with Windows 7, and they have stopped supporting the product.

So, I set upon the idea of importing all my iTunes music into Media Player - thus making it accessible to my Media Center interface. Since Media Player can handle mp3 and m4a files (and thus, 99% of my collection), I figured it wouldn't be a really big deal. But the problem as I understand it is that Media Player can't read meta data from m4a files, and so this would cause a digital divide - I would then have two music programs pointed at the same library of music and I would have to update information in both libraries and make changes and corrections in both libraries and basically this felt like a major hassle. Plus, I would have to re-enter all the meta data for my m4a files!

There's another application called Sound Bridge that takes your iTunes library and dumps it into Media Player, but, again, this doesn't work with Windows 7 and an update does not appear to be on the horizon.

Great!

Well, I thought about it and, you know, hey, I have a CS degree - admittedly a little rusty, but I have one - so I was all like, "Hey, you know what, fuck it, I'll fire up Visual Studio 2008 and I'll just write my own program!"

So, I set out to write a C# program that would take my iTunes library file and collect all the track information and meta data and file info and then build a Media Player library out of it.

And, even though I was 99% sure I would fail miserably, I've actually made some progress.

I have, at this point, a console application that reads in my iTunes XML library, parses it, and then adds all the files and meta data to a Windows Media Player playlist. Except that, for some reason, on some files, while setting the meta data in Windows Media Player, I'll get a permission denied error, as if I do not have access rights to set a specific attribute (like, say, genre) for a specific track.

I've done some investigating and it looks like I may be able to resolve this issue by way of a function called requestMediaAccessRights() - apparently according to all the demos and code samples I can find, I should be able to do the following:

myMediaPlayerObject.settings.requestMediaAccessRights("full");

Except, for some reason, Visual Studio can't find that function. I can access a host of different functions for myMediaPlayerObject.settings, but requestMediaAccessRights just isn't one of them, and I can't for the life of me figure out why. I'm adding the Media Player COM object to my project, and I can do everything else with Media Player.... that specific function just seems to be missing.

Does anyone have any idea why that might be?

The other thing I'm toying with is transferring the album art. I see from the MSDN that a Media Player media object has a "WM\Picture" attribute, which references a piece of album art, and I can get the album art out of iTunes, but I can't seem to figure out how to set the WM\Picture attribute.

Any clues?
posted by kbanas to Computers & Internet (13 answers total)
 
If Intellisense is not noticing a method that should be there, it may be that the method is actually an extension method defined in a namespace you haven't imported yet. Extension methods only become visible after you've imported the namespace they live in.
posted by a snickering nuthatch at 8:14 AM on December 1, 2009


Forgive me for this, but could the issue be casing? C# is case-sensitive, so should the call be:

myMediaPlayerObject.Settings.requestMediaAccessRights("full");
(uppercase "S" on the "Settings" object?)

If your code sample was just mistyped here on AskMe, then ignore me. :)
posted by DWRoelands at 8:19 AM on December 1, 2009


After a little more poking, it looks like requestMediaAccessRights is not an extension method. However, it would not be present if your project's reference to wmp.dll is pointing to the wrong version of the dll.
posted by a snickering nuthatch at 8:19 AM on December 1, 2009


Response by poster: However, it would not be present if your project's reference to wmp.dll is pointing to the wrong version of the dll.

And, here you lose me. I went to "Add Reference" -> COM Object -> Windows Media Player (wmp.dll). Is the DLL file specific to the version of Windows Media Player installed on the development machine? How would I get an alternate DLL file?
posted by kbanas at 8:24 AM on December 1, 2009


Which version of Windows Media Player is that DLL file from? Looks like that method is available for WMP 9 or later ( reference ). Do you have an older version installed?
posted by Lokheed at 8:35 AM on December 1, 2009


According to this, you'll want to use IWMPCore::get_settings to get an IWMPSettings object, then call IWMPSettings::QueryInterface to get an IWMPSettings2 object, which is where you'll find IWMPSettings2::requestMediaAccessRights. Intuitive, eh?

Those are all COM methods, by the way. I imagine there's a way to get at them from C#, but I couldn't tell you. And of course the posting that explained all of that claims that you shouldn't be having access problems in the first place...
posted by jedicus at 8:41 AM on December 1, 2009


To clarify - browse to the DLL (probably at c:\Windows\System32\wmp.dll), right click, choose properties, and look at the Version tab. Personally, mine is v11.0.5721.5268. I'll try throwing together a quick console app and see if I can reference it and get that method.
posted by Lokheed at 8:42 AM on December 1, 2009


Response by poster: Which version of Windows Media Player is that DLL file from? Looks like that method is available for WMP 9 or later ( reference ). Do you have an older version installed?

No. All this dev work is being done on a freshly loaded Win7 x64 box with only the latest version of WMP installed.
posted by kbanas at 8:43 AM on December 1, 2009


Response by poster: According to this, you'll want to use IWMPCore::get_settings to get an IWMPSettings object, then call IWMPSettings::QueryInterface to get an IWMPSettings2 object, which is where you'll find IWMPSettings2::requestMediaAccessRights. Intuitive, eh?

Those are all COM methods, by the way. I imagine there's a way to get at them from C#, but I couldn't tell you. And of course the posting that explained all of that claims that you shouldn't be having access problems in the first place...


Nice. I'll play with this when I get home. Yeah, I get the impression that as a locally built and executed app I shouldn't be having these problems. I get the impression that access rights are only really an issue when the app is run over over the interanet or internet or something. But, that said, the debugger is throwing permission denied errors for certain attributes on certain tracks and I can't figure out why, so I figured this was like my last ditch effort to see if it was something reasonable to fix.

Thanks for all the help.
posted by kbanas at 8:46 AM on December 1, 2009


And, here you lose me. I went to "Add Reference" -> COM Object -> Windows Media Player (wmp.dll). Is the DLL file specific to the version of Windows Media Player installed on the development machine? How would I get an alternate DLL file?
posted by kbanas at 8:24 AM on December 1 [+] [!]


COM interop isn't an area that I'm experienced with, but googling suggests that in a practical sense, the answer is yes, wmp.dll's version is tied to the version of Windows Media Player on your dev machine. It looks like Windows Updates can bump up the minor version number, but that shouldn't affect which functions are exposed by the .dll.

You should be able to download a fresh, latest-version wmp.dll from microsoft just by upgrading to the latest Windows Media Player and grabbing the latest Windows Updates.
posted by a snickering nuthatch at 8:49 AM on December 1, 2009


Best answer:
using System;
using WMPLib;
using System.Diagnostics;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
WMPLib.WindowsMediaPlayerClass wmp = new WMPLib.WindowsMediaPlayerClass();
bool foo = ((IWMPSettings2)wmp.settings).requestMediaAccessRights("full");
Debug.Write(foo.ToString());
}
}
}


That compiles for me without error, and returns true. However, it also pops a dialog box telling me that an application is requesting access rights, so you may have to take that into account.

So basicall, just case the settings object to IWMPSettings2 and you should get the requestMediaAccessRights method in intellisense.
posted by Lokheed at 9:00 AM on December 1, 2009


er, that last sentence again without typos: "So basically, just cast the settings object...."
posted by Lokheed at 9:02 AM on December 1, 2009


Just in case you don't have the resulting experience that you would like, have you checked out XMBC? Works really well, looks great, and has an iTunes plugin.

An even better equivalent for Macs is Plex.
posted by SNACKeR at 10:07 AM on December 1, 2009


« Older Help me get into Rockstar ;)   |   How to mend a broken heart Newer »
This thread is closed to new comments.