Click here to Skip to main content
Licence 
First Posted 14 Jul 2004
Views 113,386
Bookmarked 39 times

Controlling iTunes through COM

By | 14 Jul 2004 | Article
An example of how to utilize COM in C# through a system tray application that controls iTunes

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

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow Do I Search The Itune Library? Pinmembercworkman2972919:55 22 Jul '09  
Generalstreaming track from iPod to PC PinmemberHalid Niyaz5:29 23 Aug '06  
GeneralPopup window timer does not reset when track is changed while popup is showing. Pinmemberzelig6:49 26 May '06  
GeneralEvent handling Pinmemberdark_omen13:43 23 Mar '06  
AnswerRe: Event handling Pinmemberndphuong21:08 16 Sep '06  
AnswerRe: Event handling PinmemberChiser9912:02 10 Nov '06  
GeneralUsing with .NET 2.0 Framework PinmemberShawn McCartt14:31 23 Jan '06  
GeneralNot working PinmemberLudo de la Pena8:15 10 Mar '06  
GeneralRe: Not working ? PinmemberLudo de la Pena8:44 10 Mar '06  
GeneralManaging multiple invocations of the COM interop PinmemberDrew Noakes10:58 21 Jan '06  
GeneralRe: Managing multiple invocations of the COM interop PinmemberMember 23587883:24 28 Aug '08  
GeneralRe: Managing multiple invocations of the COM interop PinmemberDrew Noakes5:08 28 Aug '08  
QuestionHide iTunes Pinmembertayspen9:35 11 Nov '05  
QuestionMusic Folder PinmemberTraPpeur8:49 29 Sep '05  
AnswerRe: Music Folder PinmemberAdam Durity17:53 13 Oct '05  
GeneralRe: Music Folder PinmemberTraPpeur0:37 14 Oct '05  
GeneralRe: Music Folder PinmemberErikpro22:43 8 May '07  
GeneralAdding Tracks to iTunes Pinmembersindhoor4:25 5 Jun '05  
GeneralItunes COM Events Pinmemberzx2c414:12 25 Mar '05  
AnswerRe: Itunes COM Events PinmemberpVALIUM0:37 2 Nov '07  
Hi ZX2C4,
I know, the answer comes 2 years to late for you Wink | ;-) but i want to show my solution for this problem. (because i also was sitting a few days to get it ... and searching in the web for solutions^^)
 
#### in my main program: ####
IConnectionPointContainer* icpc;
IConnectionPoint* icp;
CiTunesEventHandler* eventHandler;
DWORD dwAdvise;
 
eventHandler = new CiTunesEventHandler();
piTunes->QueryInterface(IID_IConnectionPointContainer, (void **)&icpc);
icpc->FindConnectionPoint(DIID__IiTunesEvents, &icp);
icpc->Release();
icp->Advise(eventHandler,&dwAdvise);
icp->Release();
#### end main ####
 
#### header of my CiTunesEventHandler class ####
#include "iTunesCOMInterface.h"
 
class CiTunesEventHandler : public _IiTunesEvents
{
private:
long m_dwRefCount;
ITypeInfo* m_pITypeInfo; // Pointer to type information.

public:
CiTunesEventHandler();
~CiTunesEventHandler();
 
HRESULT OnAboutToPromptUserToQuitEvent();
HRESULT OnPlayerPlayEvent();
//...and all the functions you want to use
 
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject);
ULONG STDMETHODCALLTYPE AddRef();
ULONG STDMETHODCALLTYPE Release();
HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT *){return E_NOTIMPL;};
HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT,LCID,ITypeInfo ** ){return E_NOTIMPL;};
HRESULT STDMETHODCALLTYPE GetIDsOfNames(const IID &,LPOLESTR * ,UINT,LCID,DISPID *){return E_NOTIMPL;};
HRESULT STDMETHODCALLTYPE Invoke(DISPID dispidMember, REFIID, LCID,WORD, DISPPARAMS* pdispparams, VARIANT*,EXCEPINFO*, UINT*);
};
#### end header CiTunesEventHandler ####
 
#### implementation of CiTunesEventHandler ####
CiTunesEventHandler::CiTunesEventHandler()
{
m_dwRefCount=0;
ITypeLib* pITypeLib = NULL ;
HRESULT hr = ::LoadRegTypeLib(LIBID_iTunesLib, 1, 5, 0x00, &pITypeLib) ;
// Get type information for the interface of the object.
hr = pITypeLib->GetTypeInfoOfGuid(DIID__IiTunesEvents, &m_pITypeInfo) ;
pITypeLib->Release() ;
}
 
CiTunesEventHandler::~CiTunesEventHandler()
{
}
 
HRESULT CiTunesEventHandler::OnAboutToPromptUserToQuitEvent()
{
//iTunes send OnAboutToPromptUserToQuitEvent
return S_OK;
}
 
HRESULT CiTunesEventHandler::OnPlayerPlayEvent()
{
//iTunes send OnPlayerPlayEvent
return S_OK;
}
 
HRESULT STDMETHODCALLTYPE CiTunesEventHandler::QueryInterface(REFIID iid, void **ppvObject)
{
if ((iid == IID_IDispatch)||(iid == DIID__IiTunesEvents))
{
m_dwRefCount++;
*ppvObject = this;//(_IiTunesEvents *)this;
return S_OK;
}
if (iid == IID_IUnknown)
{
m_dwRefCount++;
*ppvObject = this;//(IUnknown *)this;
return S_OK;
}
return E_NOINTERFACE;
}
 
ULONG STDMETHODCALLTYPE CiTunesEventHandler::AddRef(){
InterlockedIncrement(&m_dwRefCount);
return m_dwRefCount;
}
 
ULONG STDMETHODCALLTYPE CiTunesEventHandler::Release(){
InterlockedDecrement(&m_dwRefCount);
if (m_dwRefCount == 0)
{
delete this;
return 0;
}
return m_dwRefCount;
}
 
HRESULT STDMETHODCALLTYPE CiTunesEventHandler::Invoke(DISPID dispidMember, REFIID, LCID,WORD, DISPPARAMS* pdispparams, VARIANT*,EXCEPINFO*, UINT*){
switch (dispidMember) //look in the documentation for "enum ITEvent" to get the numbers for the functions you want to implement
{
case 2:
this->OnPlayerPlayEvent();
//this->OnPlayerPlayEvent((IITTrack*)&(pdispparams->rgvarg[0].pdispVal));
break;
case 9:
this->OnAboutToPromptUserToQuitEvent();
break;
default: break;
}
return S_OK;
}
#### end implementation CiTunesEventHandler ####
 
the most helpful page for me, was this one: http://www11.ocn.ne.jp/~ikalu/cplus/5501.html
(by the way: Thanks to the japanies Big Grin | :-D )
 
In this way i choose, the function don't have to be named "OnPlayerPlayEvent" and so on... sounds funny (maybe it's not the correct way Roll eyes | :rolleyes: ) but that doesn't matter, because it works :-P
 
If you want to get in example the track with the OnPlayerPlayEvent... you must use something like this : "pdispparams->rgvarg[0].pdispVal" but I don't know exactly (I don't use them *g*)
 
This is a solution without using MFC Smile | :)
 
greetz pVALIUM
(sorry: if my english is not perfect, i am a German)
GeneralReceiving Events from iTunes PinsussMatt Berube8:52 6 Sep '04  
GeneralRe: Receiving Events from iTunes Pinmemberzx2c46:26 26 Mar '05  
AnswerRe: Receiving Events from iTunes PinmemberMatt Berube5:08 4 Dec '05  
QuestionRe: Receiving Events from iTunes Pinmemberslowlyhazeing210:11 5 Dec '05  
AnswerRe: Receiving Events from iTunes PinmemberpVALIUM0:45 2 Nov '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 15 Jul 2004
Article Copyright 2004 by Adam Durity
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid