I’ve been playing about with the COM interface to Apple’s iTunes running on a windows machine with .NET installed.
My choice of language has been C# as it’s a bit more Perl like than VB. I’ve used Microsoft’s “free” Visual C# Express in this case.
The iTunes SDK is available from Apple and has all the documentation needed.
I’m going to run through how to get Now Playing information out of iTunes. The quickest way to do this is as a console application.
Start up Visual C# and create a new console application. I called mine NowPlaying
.
You’ll need to add a reference to the iTunes come library. Do this on the righthand side of the C# project window by right clicking and selecting “Add Reference…”, selecting the COM tab, and finding the iTunes library.
The first thing you’ll need to in your code is to import the iTunes namespace to make the code look a little cleaner. We’ll also need the System interface for our console input and output.
using System;
using iTunesLib;
In your Main method, the first thing we need to do is to create an instance of the iTunesAppClass
. I’ve rather originally called mine, app.
// Create a new iTunesApp object to use
iTunesApp app = new iTunesAppClass();
Next we need to get the current track. This is a simple attribute call to the app object.
// Get the current track from iTunes.
ITTrack track = app.CurrentTrack;
Finally we need to show the current track. To do this we call two attributes on our track variable, name and artist. There are plenty of other attributes we could call, but these are the two most useful for this example. See the SDK for more choice.
Once we have displayed our name and artist, we need to wait for the user to hit return. This is useful if our program wasn’t launched from a console window as it would end before we’ve had a chance to read any output.
// Display some info on the current track.
Console.WriteLine("Current Track: {0}rnCurrent Artist: {1}" , track.Name, track.Artist);
// Pause until we hit return.
Console.ReadLine();
That’s it, nice and easy.
Here’s the full .cs source code.
using System;
using iTunesLib;
namespace NowPlaying
{
class Program
{
static void Main(string[] args)
{
// Create a new iTunesApp object to use
iTunesApp app = new iTunesAppClass();
// Get the current track from iTunes and return its artist and name.
IITTrack track = app.CurrentTrack;
Console.WriteLine("Current Track: {0}rnCurrent Artist: {1}" , track.Name, track.Artist);
// Pause until we hit return.
Console.ReadLine();
}
}
}