Click here to Skip to main content
5,788,961 members and growing! (20,361 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Controlling iTunes through COM

By Adam Durity

An example of how to utilize COM in C# through a system tray application that controls iTunes
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 14 Jul 2004
Updated: 14 Jul 2004
Views: 71,268
Bookmarked: 29 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 4.44 Rating: 4.11 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 8.3%
3
6 votes, 50.0%
4
5 votes, 41.7%
5

Introduction

Apple's iTunes is a powerful audio library manager and player now available for both Windows and Mac. Its feature set is endless and expanding, and, thankfully, through COM, third-party developers can access a good portion of this, even through the .NET Framework and its interop capabilities.

I have built a simple demo application to demonstrate a variety of the features that are available using the iTunes COM interface. This application, by no means, represents the full power of the interface, but is designed to show the different capabilities. Apple's developer web site has an SDK for the complete COM interface.

My application is a system tray utility for controlling iTunes easily and displaying the currently play track. It calls methods, changes properties, and receives events all through COM. It implements the basic functionality of playing, pausing, stopping and changing the currently playing track as well as popping up a window when the track changes.

Instantiating the iTunes Application

In order to access the features that iTunes has to offer, you must first add a reference to the iTunes COM Library to your project. Right click on your project in the Solution Explorer, choose "Add Reference...". Select the COM tab and find the "iTunes 1.1 Type Library", click "Select" and finally click "OK". You will now see "iTunesLib" under your References list for that project.

Next, in any files that you want to communicate with iTunes in, add the following directive:

using iTunesLib;

In order to control iTunes you must create a new instance if the controlling interface. This interface allows your program to communicate with and control (and be controlled by) and already open instance of iTunes.

private myiTunes = new iTunesAppClass();

Using this myiTunes member, you can access functions like Play(), Pause(), Stop(), and Quit(). If for some reason iTunes is closed and your application continues to try and communicate with it, a COMException will be thrown.

Receiving Events from iTunes

The myiTunes member also allows access to certain events like when a new track starts playing, when the current one stops, etc. To specify a new event handler for one of these events, iTunesLib uses the same delegate model that all other events in C# use:

// Add Event Handler

myiTunes.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(
    myiTunes_OnPlayerPlayEvent);

// Event Handler

protected void myiTunes_OnPlayerPlayEvent(object iTrack)
{
...
}

The iTrack parameter provides information about the currently playing track, but in order to get at this information, we must first cast the iTrack into an IITTrack variable.

string myArtist, myName;

IITTrack myTrack = (IITTrack) iTrack;
myArtist = myTrack.Artist;
myName = myTrack.Name;

This is by no means all of the information available in the IITTrack interface. More information is available in Apple's documentation.

Conclusion

I hope that I have merely whetted you appetite in terms of what can be done with controlling Apple's powerful audio application, iTunes. The possibilities are endless: you can batch convert your media to a different format, organize and manage playlists, and batch rename tracks, among other things. Consult Apple's SDK for more information. It is vague in terms of examples, however it contains information about all of the interfaces, what they do, and what members and functions are accessible through each.

Furthermore, I hope I have introduced you to the idea of COM (component object module) and its potentials within your own work. Whether utilizing other COM components functionality or creating your own COM components, I think you will find that it can be endlessly useful, under the right circumstances.

System Requirements

This software is known to work with the following configuration:

  • Microsoft Windows XP SP1 or later
  • Microsoft .NET Framework 1.1
  • Apple iTunes 4.6

The software may possibly work under different configurations, but this has not be verified to date.

Known Issues

  • AnimateWindow WinAPI function does not work properly for fading in the popup display.
  • Popup window timer does not reset when track is changed while popup is showing.

History

  • 07/14/2004 - Article Released

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Adam Durity



Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 31 (Total in Forum: 31) (Refresh)FirstPrevNext
Generalstreaming track from iPod to PCmemberHalid Niyaz6:29 23 Aug '06  
GeneralPopup window timer does not reset when track is changed while popup is showing.memberzelig7:49 26 May '06  
GeneralEvent handlingmemberdark_omen14:43 23 Mar '06  
AnswerRe: Event handlingmemberndphuong22:08 16 Sep '06  
AnswerRe: Event handlingmemberChiser9913:02 10 Nov '06  
GeneralUsing with .NET 2.0 FrameworkmemberShawn McCartt15:31 23 Jan '06  
GeneralNot workingmemberLudo de la Pena9:15 10 Mar '06  
GeneralRe: Not working ?memberLudo de la Pena9:44 10 Mar '06  
GeneralManaging multiple invocations of the COM interopmemberDrew Noakes11:58 21 Jan '06  
GeneralRe: Managing multiple invocations of the COM interopmemberMember 23587884:24 28 Aug '08  
GeneralRe: Managing multiple invocations of the COM interopmemberDrew Noakes6:08 28 Aug '08  
QuestionHide iTunesmembertayspen10:35 11 Nov '05  
QuestionMusic FoldermemberTraPpeur9:49 29 Sep '05  
AnswerRe: Music FoldermemberAdam Durity18:53 13 Oct '05  
GeneralRe: Music FoldermemberTraPpeur1:37 14 Oct '05  
GeneralRe: Music FoldermemberErikpro23:43 8 May '07  
GeneralAdding Tracks to iTunesmembersindhoor5:25 5 Jun '05  
GeneralItunes COM Eventsmemberzx2c415:12 25 Mar '05  
AnswerRe: Itunes COM EventsmemberpVALIUM1:37 2 Nov '07  
GeneralReceiving Events from iTunessussMatt Berube9:52 6 Sep '04  
GeneralRe: Receiving Events from iTunesmemberzx2c47:26 26 Mar '05  
AnswerRe: Receiving Events from iTunesmemberMatt Berube6:08 4 Dec '05  
QuestionRe: Receiving Events from iTunesmemberslowlyhazeing211:11 5 Dec '05  
AnswerRe: Receiving Events from iTunesmemberpVALIUM1:45 2 Nov '07  
GeneralMac equivalentsussAnonymous14:51 24 Aug '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jul 2004
Editor: Nishant Sivakumar
Copyright 2004 by Adam Durity
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project