Results 1 to 15 of 15

Thread: event handling com objects - newbee

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: event handling com objects - newbee

    the problem is because you're using late binding.
    you need to declare iTunes as an explicit type

  3. #3

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: event handling com objects - newbee

    this part:

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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!

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: event handling com objects - newbee

    instead of CreateObject, there's another method for recognizing an already open app. GetObject.
    try googling that...

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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...

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width