Wednesday, January 13, 2010

Finally figured out how to cast IITFileOrCDTrack in Java with Jacob

I'm moving away from the iTunesController package as everytime it's used the Com object stays active and iTunes complains when you want to close it.  Also it seems to be a source of memory leaks.  Speaking of which all are fixed now.  Anyway, it was difficult to get rid of the iTunesController package because it allowed me to cast to an ITFileOrCDTrack which was needed to get rid of orphaned files.  But now...I've got it working with more pure Jacob code.

Its inspired by the Limewire program source.  The code is open source and written in Java and therefore much of the iTunes code is now based on that code.  Here's the code I'm using to remove orphaned tracks.  First you need the iTunes Jar file from limewire.  You can download the source and it's included in the folder:  limewire/lib/jars/windows

Then use this code..

ActiveXComponent iTunesCom = new ActiveXComponent(ItunesMediator.ITUNES_ACTIVEX_NAME);
Dispatch iTunesController = iTunesCom.getObject();
IiTunes it = new IiTunes(iTunesController);
IITLibraryPlaylist pl = it.getLibraryPlaylist();
IITTrackCollection coll = pl.getTracks();
ArrayList orphanedFiles = new ArrayList();
int count = coll.getCount();
for(int i=1; i<=count; i++){ if(mon.isCanceled()){ break; } IITTrack track = coll.getItem(i); if(track.getKind() == ITTrackKind.ITTrackKindFile){ IITFileOrCDTrack file = new IITFileOrCDTrack(track); if(file != null && file.getLocation().equals("")){ orphanedFiles.add(file); } } mon.setProgress( (int)(((double)i / (double)count) * 50) ); }

No messy QueryInterface method stuff.  Since there was no JavaDoc on the iTunes jar and the jar itself is a Jar, it took forever before I just tried this.  But it works.  Hope this helps anyone else looking for info on this.


No comments:

Post a Comment