Reading from ITunes

Apple ITunes have a huge COM object API, from where you can control ITunes. The API are of course accessible from C# and .NET simply by adding a reference to the COM library. The ITunes library is called “iTunes 1.11 Type Library” (or whatever version of ITunes you have running).

One thing you need to know: When you create a new instance of the ITunes app, the ITunes application will load in the background. To avoid this, you can check to see if the ITunes application is running, and only if it is create the ITunes instance. This is a template class that instantiates an ITunes object only if ITunes is running.

using System;
 

namespace Pentia.ITunes
{
  internal class ITunesReader : IDisposable
  {
    /// <summary>
    /// The ITunes COM application
    /// </summary>
    private iTunesLib.iTunesAppClass _app = null;

    /// <summary>
    /// Initializes a new instance of the <see cref="ITunesReader"/> class.
    /// </summary>
    public ITunesReader()
    {
      // See if the itunes process is running.
      if (System.Diagnostics.Process.GetProcessesByName("iTunes").Count() > 0)
        _app = new iTunesLib.iTunesAppClass();
    }

    /// <summary>
    /// Gets a value indicating whether this instance is active.
    /// </summary>
    /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
    public bool IsActive
    {
      get
      {
        return _app != null;
      }
    }

    #region IDisposable Members

    /// <summary>
    /// Performs application-defined tasks associated with freeing,
    /// releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
      if (_app != null)
      {
        // Release the com object by setting the reference counter to 0 
        Marshal.FinalReleaseComObject(_app);
      } 
    }

    #endregion
  }
}

Please notice that I use the IDisposable interface in order to release my reference to ITunes.

Now, lets see if ITunes is playing:

_app.PlayerState == iTunesLib.ITPlayerState.ITPlayerStatePlaying;

The ITunes library is not perfect. For example, _app.CurrentTrack is describing the current track playing, but you cannot see if it is playing a radio station or a song from the library. This is how I solved the radio/song issue:

if (_app.PlayerState == iTunesLib.ITPlayerState.ITPlayerStatePlaying)
{
 if (_app.CurrentStreamTitle != null)
 {
   // We are listening to a radio station
   Console.WriteLine("Radio Station name: " + _app.CurrentTrack.Name);
   Console.WriteLine("Track: " + _app.CurrentStreamTitle);
 } 
 else
 {
   // We are listening to a song
   Console.WriteLine("Track: " + _app.CurrentTrack.Name);
   Console.WriteLine("Artist: " + _app.CurrentTrack.Artist);
   Console.WriteLine("Album: " + _app.CurrentTrack.Album);
   Console.WriteLine("Length (in seconds): " + _app.CurrentTrack.Duration.ToString());
   Console.WriteLine("Position (in seconds): " _app.PlayerPosition.ToString());
 }
}

You can do much more that this. You can start/stop songs, create your own playlists and even convert your songs to new formats. Explore the API for yourself. I wish you happy ITunes’ing.

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in General .NET and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s