2

I have some problems to understand the iTunes COM interface with Jacob in reference to the PersistentIDs (PersistentIDLow and PersistentIDHigh) to identify playlists and tracks by unique key in iTunes.

I hope someone can tell me what I'm doing wrong.

Here is my code example to read one playlist List 1 of iTunes. The example shows in comments the trial to read the PersistentIDs for the playlist List 1 and for every track of the playlist.

But the Dispatch doesn't work, see code lines with comment "doesn't work".

Exception in thread "main" com.jacob.com.ComFailException: Can't map name to dispid: GetObjectPersistentIDLow

Exception in thread "main" com.jacob.com.ComFailException: Can't map name to dispid: GetObjectPersistentIDHigh

public static void main(String args[]){
    System.setProperty("jacob.dll.path", "C:\\Dev\\iTunesTest\\dll\\jacob-1.18-M2-x86.dll");
    
    // build iTunes activeX component
    ActiveXComponent g_iTunes = new ActiveXComponent("iTunes.Application");
    // get iTunes Sources
    Dispatch g_sources = Dispatch.call(g_iTunes, "Sources").toDispatch(); 
    // determine iTunes Library Source ID
    int sourceID = 0;
    for (int i=1;i<=Dispatch.get(g_sources, "Count").getInt();i++){  
        Dispatch itemSearch = Dispatch.call(g_sources, "Item", i).toDispatch();
        if (ITunesSourceKind.values()[Dispatch.get(itemSearch, "Kind").getInt()].toString().equals("ITSourceKindLibrary")){
            sourceID = Dispatch.get(itemSearch, "Index").getInt();
        }
        itemSearch.safeRelease();
    }
    // get iTunes Library Source
    Dispatch g_source = Dispatch.call(g_sources, "Item", sourceID).toDispatch();
    // get iTunes Playlists
    Dispatch g_playlists = Dispatch.get(g_source, "Playlists").toDispatch();    
    // get iTunes Playlist byName
    Dispatch g_playlistItem = Dispatch.call(g_playlists, "ItemByName", "List 1").toDispatch();
    // get iTunes Tracks for Playlist
    Dispatch g_tracks = Dispatch.get(g_playlistItem, "Tracks").toDispatch();   
                
    String name = Dispatch.get(g_playlistItem, "Name").getString();
    System.out.println("Playlist: " + name);
    // doesn't work
    System.out.println("ID Low  : " + Dispatch.call(g_playlistItem, "GetObjectPersistentIDLow").getLong());
    System.out.println("ID High : " + Dispatch.call(g_playlistItem, "GetObjectPersistentIDHigh").getLong());
        
    // get every iTunes playlist track
    int playlistSongCount = Dispatch.get(g_tracks, "Count").getInt();
    for (int i=1; i<=playlistSongCount; i++){
        // get single iTunes playlist track
        Dispatch track = Dispatch.call(g_tracks, "Item", i).toDispatch();
        System.out.println(" Song Name: " + Dispatch.get(track, "Name").getString());
        // doesn't work
        System.out.println(" ID Low   : " + Dispatch.call(track, "GetObjectPersistentIDLow").getLong());
        System.out.println(" ID High  : " + Dispatch.call(track, "GetObjectPersistentIDHigh").getLong());
        if(track != null){
            track.safeRelease();
        }
    }
    
    // release objects
    if(g_tracks != null){
        g_tracks.safeRelease();
    }
    if(g_playlistItem != null){
        g_playlistItem.safeRelease();
    }
    if(g_playlists != null){
        g_playlists.safeRelease();
    }
    if(g_source != null){
        g_source.safeRelease();
    }
    if(g_sources != null){
        g_sources.safeRelease();
    }
    if(g_iTunes != null){
        // close iTunes
        //g_iTunes.invoke("Quit");          
        g_iTunes.safeRelease();
    }
}

I thought this should work, see reference url Java-iTunes-API

Summary:

    /**
 * Returns the high 32 bits of the persistent ID of the specified IITObject.
 * See the documentation on IITObject for more information on persistent
 * IDs.
 * 
 * The object may be a source, playlist, or track.
 * 
 * @param iObject
 *            The object to fetch the High Persistent ID.
 * @return The high 32 bits of the 64-bit persistent ID.
 */
public long getITObjectPersistentIDHigh(ITObject iObject) {
    Dispatch object = iObject.fetchDispatch();
    return Dispatch.call(object, "GetObjectPersistentIDHigh", object)
            .getLong();
}

/**
 * Returns the low 32 bits of the persistent ID of the specified IITObject.
 * See the documentation on IITObject for more information on persistent
 * IDs.
 * 
 * The object may be a source, playlist, or track.
 * 
 * @param iObject
 *            The object to fetch the Low Persistent ID.
 * @return The low 32 bits of the 64-bit persistent ID.
 */
public long getITObjectPersistentIDLow(ITObject iObject) {
    Dispatch object = iObject.fetchDispatch();
    return Dispatch.call(object, "GetObjectPersistentIDLow", object)
            .getLong();
}

Any idea what I'm doing wrong?

Thank you so much.

Michael

Community
  • 1
  • 1
Michael
  • 71
  • 1
  • 5

0 Answers0