event handling com objects - newbee
Hi
I use the COM interface of iTunes to control the program and add features. This works well. I can't get event handling to work, though.
The events that iTunes can raise are listed here: http://www.joshkunz.com/iTunesContro...nesEvents.html
I want to handle the OnPlayerPlayEvent. This is my code. The iTunes object is created properly because all regular functions work, and I use this bit of code in all my iTunes related programs. When I add the addhandler, I get an error message saying 'OnPlayerPlayEvent' is not an event of 'Object'. Can anyone see what I'm doing wrong? It is clearly listed in the interface documentation (see link)
Thank you!
Module iTunesCom
'The COM object used to interact with iTunes
Public iTunes As Object
Sub OpenITunes()
On Error Resume Next
iTunes = CreateObject("ITunes.Application")
If Err.Number <> 0 Then
MsgBox("Error: unable to open iTunes.")
End If
On Error GoTo 0
AddHandler iTunes.OnPlayerPlayEvent, AddressOf iTrackHandler
End Sub
Sub VerifyITunes()
If iTunes Is Nothing Then
OpenITunes()
End If
End Sub
Public Sub iTrackHandler(ByVal iTrack As Object)
MsgBox("A new track has started playing") 'just testing
End Sub
End Module
Re: event handling com objects - newbee
the problem is because you're using late binding.
you need to declare iTunes as an explicit type
Re: event handling com objects - newbee
Re: event handling com objects - newbee
Thank you Paul,
Was your second post meant to help me understand how to declare iTunes as an explicit type? (not a bad idea, because currently I don't know how, but I'll try to educate myself about this)
However: the person writing that post states: "One item I can not get to work is the iTunes events, running it through the debugger shows that the event subroutines get fired but do not entirely execute for some reason; complete mystery, and if anyone solves, it I would be interested." So maybe you added the link to tell me it can't be done...
However, I did find this post: http://www.codeproject.com/Articles/...es-through-COM from someone who's got the events to work in c#. You would think that if it can work in c# it can work in VB too.
Thanks for any further thoughts you might have.
King regards
Re: event handling com objects - newbee
this part:
Quote:
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();
that translates to: add the reference, import iTunesLib, then:
Code:
private myiTunes as new iTunesAppClass
Re: event handling com objects - newbee
I've added the reference and added Imports iTunesLib. No problem.
When I add your code 'private myiTunes as new iTunesAppClass', I get an error message saying: "Interop type 'iTunesAppClass' cannot be embedded. Use the applicable interface instead."
Not too sure how to correct this... Any suggestions?
Thanks!
Re: event handling com objects - newbee
try this:
Code:
Imports iTunesLib
Public Class Form1
'The COM object used to interact with iTunes
Public iTunes As iTunesApp
Sub OpenITunes()
On Error Resume Next
iTunes = DirectCast(CreateObject("ITunes.Application"), iTunesApp)
If Err.Number <> 0 Then
MsgBox("Error: unable to open iTunes.")
End If
On Error GoTo 0
AddHandler iTunes.OnPlayerPlayEvent, AddressOf iTrackHandler
End Sub
Sub VerifyITunes()
If iTunes Is Nothing Then
OpenITunes()
End If
End Sub
Public Sub iTrackHandler(ByVal iTrack As Object)
MsgBox("A new track has started playing") 'just testing
End Sub
End Class
Re: event handling com objects - newbee
YES!!
This does the trick. Thank you very much for your persistence. Much appreciated!
This opens the door to a whole range of new features.
Kind regards
Re: event handling com objects - newbee
Hi Paul,
The code you supplied works like I hoped it would. There seems to be one issue left to tackle though: if I close the app, leave iTunes running, and then launch the app again, the events are not handled.
Do you know what might cause this? Do you know of a way reset/release any event handling that may still be going on in the background (even though the app was closed)? I googled, but didn't find anything (that I could understand well enough)
I appreciate any thoughts you might have.. Thanks
Re: event handling com objects - newbee
instead of CreateObject, there's another method for recognizing an already open app. GetObject.
try googling that...
Re: event handling com objects - newbee
Thanks. looked it up.
I launched the version of the app as I had it before. Then closed it. The assumption is that the object is still alive, because relaunching the app results in event handling no longer working. So instead, I before I launched the app again I replaced
iTunes = DirectCast(CreateObject("ITunes.Application"), iTunesApp)
with
iTunes = DirectCast(GetObject("ITunes.Application"), iTunesApp)
This results in an error. So either the assumption is wrong or I have misunderstood you or implemented it wrong.
What do you think? Thanks
Re: event handling com objects - newbee
try:
Code:
iTunes = TryCast(GetObject(, "ITunes.Application"), iTunesApp)
if iTunes is nothing then
'no running app
'use createobject here
end if
Re: event handling com objects - newbee
It seems that closing the application also gets rid off the iTunes object. I have this code now:
iTunes = TryCast(GetObject(, "ITunes.Application"), iTunesApp)
If iTunes Is Nothing Then
MsgBox("app not running yet")
iTunes = DirectCast(CreateObject("ITunes.Application"), iTunesApp)
End If
When I close my application and relaunch it with iTunes still open, I always get the "app not running yet" message, so the iTunes Object is always newly created, there isn't one waiting to be (re)used. But as previously, the event handling no longer works after a relaunch.
I am a tango-DJ, and I use iTunes for this. Because people dance to the music I play, once iTunes is running, I can't shut it down for a relaunch to re-enable the event handling. Otherwise it wouldn't have been such an critical issue.
Do you have any other ideas what might be causing this? Or is it time to consider a workaround? I'm thinking of a timer job to look for the changes that I would have hoped to monitor with event handling.
Thanks.
Re: event handling com objects - newbee
i'm not 100% sure what's happening there + i'm not in a position to test it.
maybe someone else can help you solve that...
Re: event handling com objects - newbee
No worries. Your help has been very valuable and I have learned a lot whilst looking at your suggestions. Maybe somebody else will have input, although it seems that very few people use the iTunes COM interface, particularly with VB.
As I mentioned I will try to solve this with a timer function that checks for changes every second. Not as elegant as events but it will probably get the job done, too.
Thanks Paul