iTunes Now Playing in VB 2005

As I’ve been using VB 2005 a lot more a work the past few weeks, I thought I’d rewrite my iTunes Now Playing console app in VB.

It does the same as the previous C# example, and again I’ve chosen to use the free version of the language and Microsoft’s Visual Basic 2005 Express Edition.

The first thing to do, is to make sure you have downloaded the iTunes SDK from Apple.

In Visual Basic, create a new Console Application. We need to add a reference to the iTunes COM library, so we do this by right clicking over our application in the Solution Explorer, and selecting “Add Reference…”. Select the COM tab, and scroll down to find the iTunes library. Mine was called “iTunes 1.7 Type Library”.

adding the iTunes COM object to VB 2005

Now for some code.

Firstly, like all good programmers we turn on VB’s strict mode to make sure we’re not using too many bad programming practices.

Option Strict On

Next we need to import the libraries we want to use. In this case, System> and iTunesLib.

Imports System
Imports iTunesLib

As we’ve created a Console App in VB, we can just drop the following code into the Main sub.

Firstly we need to create an iTunesApp object so we can get talk to iTunes. After that we need to get the current track.

Dim app As New iTunesApp()
Dim track As IITTrack = app.CurrentTrack

We should now have the details of the current track in the track object so we just need to print these to the console.

Console.WriteLine("Current Track: {0}", track.Name)
Console.WriteLine("Current Artist: {0}", track.Artist)

Finally, just so we can see the result we need to wait for the user to press return before we exit.

Console.ReadLine()

Fire up iTunes if it’s not already on and start a track playing. Now start your VB program, and it should tell you the current track title and artist as shown below.

iTunes now playing script

It’s as simple as that. Obviously I’m not checking for exceptions and errors as this is just an basic example.

Your final code should look something like this.

Option Strict On
Imports System
Imports iTunesLib
Module Module1
Sub Main()
Dim app As New iTunesApp()
Dim track As IITTrack = app.CurrentTrack
Console.WriteLine("Current Track: {0}", track.Name)
Console.WriteLine("Current Artist: {0}", track.Artist)
Console.ReadLine()
End Sub
End Module