One area in which iTunes is sorely lacking is the ability to automatically recognize changes to the physical song files within the library and update itself accordingly. Windows Media Player (and most other media players on the face of the planet) do this naturally. A new file arrives somewhere under C:\Users\Kevin\Music and WMP automatically updates the library to reflect the addition. Likewise when deleting files.
I recently equipped all members of Casa dé Hambone with iPods of some fashion. The kids each received an iPod Shuffle. Mrs. Hambone received an iPod Nano. And I traded in a non-working 40GB iPod for an iPod Nano as well.
My challenge was to automate the process of importing songs into a secondary iTunes library. I wanted to rip CDs from the comfort of my desk and have the songs automatically appear within iTunes on my wife's computer. Furthermore, I wanted to rip the CDs to WMA using Windows Media Player and have iTunes convert them to AAC on the import.
Oddly enough, Apple provides an entire COM-based iTunes SDK that enables you to extensively automate iTunes, including the conversion process. I must say that I'm really impressed that Apple has gone to this extent, especially on a Windows system.
For my purposes, the iTunesAppClass.ConvertFiles2 method seemed like just the method to use. As you can drag and drop an entire list of WMAs into iTunes, and iTunes will gracefully begin a batch conversion process, it only made sense that this method was exactly the thing for which I was looking. The only "problem" was the ConvertFiles2 method signature:
iTunesConvertOperationStatus ConvertFiles2(ref object filePaths)
Huh? It would make sense that ConvertFiles2 would take an array of strings, but a ref object? Attempting to pass an array of strings to ConvertFiles2 - as if I by sheer willpower I expected this to work - results in a "cannot convert from string[] to ref object" error at compile time:
string[] files = new string[] { ... }; iTunes.ConvertFiles2(files);
And trying to cast an array of strings to a ref object on method call results in "A ref or out argument must be an assignable variable" at compile time:
string[] files = new string[] { ... }; iTunes.ConvertFiles2(ref ((object) files));
Not a good sign.
So what to do? Well, it turns out that with casting prior to the method call, we can coerce an array of strings into an object and then pass a reference to that object:
string[] files = new string[] { ... }; object filesAsObject = (object)files; iTunes.ConvertFiles2(ref filesAsObject);
In this instance, I believe I'm fortunate that ConvertFiles2 does not actually modify its incoming list of files. Instead, we see a less-than-stellar method signature resulting from COM interop and there's not much we can do about it, but it does work.
The result is a Windows application which sits politely in the Taskbar Notification Area waiting for new WMA files to arrive and then, en masse, hands them over to iTunes for conversion to AAC. I can now rip kid-friendly music from my comfy chair to a shared drive on the casadehambone.com network and have it magically appear within iTunes on my wife's computer where it is then synchronized down to the kids iPods.
Near nirvana.
I'll gladly post the application and its source code as soon as I can come up with a name for this handy little application. All of the usual suspects (iTunesCommander, iTunesImporter, iTunesAgent) seem to already exist. Any ideas?
Powered by: newtelligence dasBlog 1.9.6264.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2008, Kevin W. Hammond
E-mail