views:

19

answers:

0

I'm making a utility that allows me to control iTunes (via the COM library) with hotkeys. The hotkeys work, and control of iTunes, like next and previous track all work.

The other part of this utility is to show notifications for when the song changes. Through some reading and experimentation, I found that the right event on the iTunesAppClass was OnPlayerPlayEvent, and so when I hooked that up to tray notifications, it worked absolutely perfectly.

The problem I have is that an overwhelming majority of the time I run my program, that event doesn't seem to be raised at all. Rarely, however, without any changes to my code, the event is raised and my tooltip shows up.

This is how I setup my iTunes object:

itunes = new iTunesAppClass();  
itunes.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(itunes_OnPlayerPlayEvent);

And this is the event handler:

private void itunes_OnPlayerPlayEvent(object iTrack)
{
    IITTrack track = iTrack as IITTrack;
    CurrentTrack = track;

    // ... notification.
}

Any ideas?

EDIT: After trying it out some more, it seems the events fire when a fresh instance iTunes opens and my application connects to that instance for the first time. That is to say that if both iTunes and my utility (with working notifications) is open, should I close and reopen my utility, its notifications will not work. However, if I close and reopen iTunes, then my utility, notifications will work.

This seems to be the problem, since if I close my utility, and then I try to close iTunes, iTunes will tell me that the scripting interface is still being used by applications (even though it isn't).

So it seems that either iTunes will only fire the event for the first registered application, or that iTunes is trying to fire the event for an non-existent application, fails, and stops before it fires it for the other applications.

So what I should be asking is, is there a way to unregister my application with iTunes upon exit? Or maybe even unregister all the registered applications except mine?